标签:return 线性表 temp int next 链表 循环 LinkedNode CLinkedList
#include<iostream>
#define ERROR 0
#define OK 1
#define NOTFOUND 0
using namespace std;
typedef char ElemType;
/*定义结点和循环链表*/
typedef struct LinkedNode {
ElemType data;
LinkedNode *next;
} CLinkedList;
/*初始化循环链表*/
void InitList(CLinkedList &L){
L.next = &L;
}
/*检查循环链表是否为空*/
bool ListEmpty(CLinkedList L){
return (L.next == &L);
}
/*取得链表中索引对应的地址*/
LinkedNode *getAddress(CLinkedList &L,int i){
LinkedNode *temp = &L;
for(int j=0;j<i;j++){
temp = temp->next;
}
return temp;
}
/*返回链表已有元素数量*/
int ListLength(CLinkedList &L){
LinkedNode *temp = &L;
int c =0;
while(temp->next!=&L){
temp = temp->next;
c++;
}
return c;
}
/*取得第i的元素,并将其代入e中*/
int GetElem(CLinkedList &L,int i,ElemType &e){
if(ListEmpty(L)||!(i>=1&&i<= ListLength(L))) return ERROR;
e = getAddress(L,i)->data;
return OK;
}
/*返回第一个数据域为x的结点所在索引*/
int LocateElem(CLinkedList &L,ElemType x){
if(ListEmpty(L)) return NOTFOUND;
LinkedNode *temp = L.next;
int i=1;
while(temp!=&L&&temp->data!=x){
i++;
temp = temp->next;
}
if(i== ListLength(L)+1) return NOTFOUND;
return i;
}
/*使用头插法插入单个结点,返回插入结点的地址*/
LinkedNode *ListInsertL(CLinkedList &L,ElemType x){
LinkedNode *toAdd = new LinkedNode;
toAdd->data = x;
toAdd->next = L.next;
L.next = toAdd;
return toAdd;
}
/*使用头插法插入由数组组成的多个结点,n代表数组的长度*/
int ListArrInsertL(CLinkedList &L,ElemType arr[],int n){
for(int i=0;i<n;i++){
ListInsertL(L,arr[i]);
}
return OK;
}
/*使用尾插法插入单个结点,返回插入结点的地址*/
LinkedNode *ListInsertR(CLinkedList &L,ElemType x){
LinkedNode *toAdd = new LinkedNode;
toAdd->data = x;
LinkedNode *temp = getAddress(L, ListLength(L));
toAdd->next = temp->next;
temp->next = toAdd;
return toAdd;
}
/*使用尾插法插入由数组组成的多个结点,n代表数组的长度*/
int ListArrInsertR(CLinkedList &L,ElemType arr[],int n){
for(int i=0;i<n;i++){
ListInsertR(L,arr[i]);
}
return OK;
}
/*删除第i个元素,并将其数据带回e中*/
int DelElem(CLinkedList &L,int i,ElemType &e){
/*指针p指向第i-1个元素*/
LinkedNode *p = getAddress(L,i-1);
/*指针q指向第i个元素*/
LinkedNode *q = p->next;
p->next = q->next;
/*将被删除结点q的数据代入e中*/
e = q->data;
/*在内存空间中释放结点q*/
free(q);
}
/*展示双链表所有元素*/
void DispLinkedList(CLinkedList &clinkedList) {
if(ListEmpty(clinkedList)) return;
/*temp指针用于遍历链表中所有的数据元素*/
LinkedNode *temp = clinkedList.next;
cout << &clinkedList << endl;
int i = 1;
while(temp!=&clinkedList){
/*输出移动变量的地址和循环链表头指针的地址*/
cout << "temp:" << temp << " | &clinkedList:" << &clinkedList << endl;
/*输出数据元素以及所在索引*/
cout<<"elment "<<i++<<" : "<<temp->data<<endl;
temp = temp->next;
}
cout<<endl<<"while-loop end"<<endl;
/*循环结束,对比移动变量和双链表头指针的地址*/
cout << "temp:" << temp << " &clinkedList :" << &clinkedList << endl<<endl;
}
int main(){
ElemType e;
int location;
/*创建并初始化双链表L*/
CLinkedList L,*pL;
InitList(L);
ElemType arrL[] = {'a','b','c'};
/*使用头插法依次插入三个结点*/
ListArrInsertL(L,arrL,3);
ElemType arrR[]={'L','U','T'};
/*使用尾插法依次插入三个结点*/
ListArrInsertR(L,arrR,3);
/*输出双链表所有数据元素*/
DispLinkedList(L);
cout<<"The length of the CLinkedList is :"<<ListLength(L)<<endl;
cout<<"The element 'c' is at the index of :"<<LocateElem(L,'c')<<endl;
cout<<endl<<"========================================"<<endl<<endl;
DelElem(L,3,e);
DispLinkedList(L);
cout<<"The length of the CLinkedList is :"<<ListLength(L)<<endl;
if(location = LocateElem(L,'a')) cout<<"The element 'a' is at the index of :"<<location<<endl;
else cout<<"element 'a' not found."<<endl;
}
标签:return,线性表,temp,int,next,链表,循环,LinkedNode,CLinkedList 来源: https://www.cnblogs.com/c0ngee/p/15361637.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。