链表操作(单双、循环、静态链表)
以下c++代码都已经在CodeBlock成功运行
一、单链表
//单链表操作 不难但还是自己实现一边
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
Node *List_HeadInsert(Node *head)
{
Node *pnew;
head=new Node;
head->data=0;
head->next=nullptr;
//cout<<head->data<<" ";
int i=0;
while(i<10)
{
pnew=new Node;
pnew->data=i+1;
i++;
pnew->next=head;
head=pnew;
}
}
Node *List_NonHeadInsert(Node *Lnonhead)
{
Node *pnew;
Lnonhead=nullptr;
int i=0;
while(i<10)
{
pnew=new Node;
pnew->data=i+1;
i++;
pnew->next=Lnonhead;
Lnonhead=pnew;
}
return Lnonhead;
}
Node *List_Insert(Node *l,int v,int i)
{
Node *h=l;
for(int j=0;j<i-1;j++)
{
h=h->next;
}
Node *pnew=new Node;
pnew->data=v;
pnew->next=h->next;
h->next=pnew;
return l;
}
Node *List_DeleteNode(Node *l,int i)
{
Node *h=l;
for(int j=0;j<i-1;j++)
{
h=h->next;
}
Node *pnext=h->next;
h->next=pnext->next;
delete pnext;
return l;
}
Node *List_TailInsert(Node *head)
{
head=new Node;
head->data=0;
head->next=nullptr;
Node *pnew,*r=head;
int i=0;
while(i<10)
{
pnew =new Node;
pnew->data=i+1;
pnew->next=nullptr;
i++;
r->next=pnew;
r=pnew;
}
return head;
}
Node *GetElem(Node *head,int n)
{
Node *pnow=head;
for(int i=0;i<n;i++)
{
pnow=pnow->next;
}
return pnow;
}
Node *LocateElem(Node *head,int v)
{
Node *pnow=head;
while(pnow!=nullptr)
{
if(pnow->data==v)
{
return pnow;
}
pnow=pnow->next;
}
return nullptr;
}
void PrintLinkList(Node *n)
{
Node *h=n;
while(h!=nullptr)
{
cout<<h->data<<" ";
h=h->next;
}
cout<<endl;
}
int ListCount(Node *l)
{
int k=0;
Node *p=l;
while(p!=nullptr)
{
p=p->next;
k++;
}
return k-1;
}
int main()
{
cout<<"该例子中0为头节点的信息"<<endl;
Node *Lhead;
Lhead=List_HeadInsert(Lhead);
cout<<"头插法带头结点:"<<endl;
PrintLinkList(Lhead);
Node *Lnonhead;
Lnonhead=List_NonHeadInsert(Lnonhead);
cout<<"头插法不带头结点:"<<endl;
PrintLinkList(Lnonhead);
Node *Tailhead;
Tailhead=List_TailInsert(Tailhead);
cout<<"尾插法带头结点:"<<endl;
PrintLinkList(Tailhead);
cout<<"(尾插法建立的链表中)查找第i个元素,i设定5:";
Node *p1=GetElem(Tailhead,5);
cout<<p1->data<<endl;
cout<<"(尾插法建立的链表中)是否查找到元素value,value设定为7:";
Node *p2=LocateElem(Tailhead,7);
if(p2!=nullptr)
cout<<"Find!"<<endl;
else cout<<"Not Find"<<endl;
cout<<"(尾插法建立的链表中)插入元素,value设定为5,位置为3:"<<endl;
Node *p3=List_Insert(Tailhead,5,3);
PrintLinkList(p3);
cout<<"(尾插法建立的链表中)删除元素,删除第5个元素:"<<endl;
Node *p4=List_DeleteNode(Tailhead,5);
PrintLinkList(p4);
cout<<"上一个链表长度(不计算头节点):"<<ListCount(p4)<<endl;
cout<<"End!";
return 0;
}
二、双链表
//为了方便就不用输入了,关键代码对了就行了
#include <iostream>
using namespace std;
typedef int ElemType;
struct Node
{
ElemType data;
Node *pre,*next;
};
Node *DLinkList_Create(Node *l,int len)
{
l=new Node;
l->data=0;
l->next=nullptr;
l->pre=nullptr;
Node *pnew,*r=l;
for(int i=0; i<len; i++)
{
pnew=new Node();
pnew->data=i+1;
pnew->pre=r;
r->next=pnew;
pnew->next=nullptr;
r=pnew;
}
return l;
}
void PrintElem(Node *l)
{
cout<<"顺序输出:";
Node *p=l,*pr;
while(p!=nullptr)
{
cout<<p->data<<" ";
pr=p;
p=p->next;
}
cout<<endl;
cout<<"逆序输出:";
while(pr!=nullptr)
{
cout<<pr->data<<" ";
pr=pr->pre;
}
cout<<endl;
}
Node *DLinkList_Delete(Node *l,int i)
{
Node *p=l;
for(int j=0;j<i-1;j++)
{
p=p->next;
}
Node *pnext=p->next;
pnext->next->pre=p;
p->next=pnext->next;
delete pnext;
return l;
}
Node *DLinkList_Insert(Node *l,int v,int i)
{
Node *p=l;
for(int j=0;j<i-1;j++)
{
p=p->next;
}
Node *pnew=new Node;
pnew->data=v;
pnew->pre=p;
p->next->pre=pnew;
pnew->next=p->next;
p->next=pnew;
return l;
}
int main()
{
Node *s;
s=DLinkList_Create(s,10);
PrintElem(s);
cout<<"(不算第一个头节点)在第五个位子上插入数值-1:"<<endl;
s=DLinkList_Insert(s,-1,5);
PrintElem(s);
cout<<"(不算第一个头节点)删除在第四个位子上数值:"<<endl;
s=DLinkList_Delete(s,4);
PrintElem(s);
return 0;
}
三、循环链表
1.循环单链表
//0为头结点的信息,可建立不带头结点的链表,其他操作和单链表类似就不重复写了
#include <iostream>
using namespace std;
typedef int ElemType;
struct Node
{
ElemType data;
Node *next;
};
Node *CircularLinkList_Create(Node *r,int len)
{
r=new Node;
r->data=0;
r->next=nullptr;
Node *pnew,*p=r;
for(int i=0;i<len;i++)
{
pnew=new Node;
pnew->data=i+1;
p->next=pnew;
p=pnew;
}
p->next=r;
return p;
}
void PrintTwiceElem(Node *r,int i)
{
Node *p=r;
for(int j=0;j<(i+1)*2;j++)
{
p=p->next;
cout<<p->data<<" ";
}
cout<<endl;
}
int main()
{
Node *r;
r=CircularLinkList_Create(r,10);
PrintTwiceElem(r,10);
return 0;
}
2.循环双链表
//0为头结点的信息,循环双链表建立和双链表一样,别忘了头节点和最后一个点要连起来
#include <iostream>
using namespace std;
typedef int ElemType;
struct Node
{
ElemType data;
Node *next,*pre;
};
Node *CircularLinkList_Create(Node *r,int len)
{
r=new Node;
r->data=0;
r->next=nullptr;
r->pre=nullptr;
Node *pnew,*p=r;
for(int i=0;i<len;i++)
{
pnew=new Node;
pnew->data=i+1;
pnew->next=nullptr;
pnew->pre=p;
p->next=pnew;
p=pnew;
}
p->next=r;
r->pre=p;
return p;
}
void PrintTwiceElem(Node *r,int i)
{
cout<<"正序输出2边:"<<endl;
Node *p=r;
for(int j=0;j<(i+1)*2;j++)
{
p=p->next;
cout<<p->data<<" ";
}
cout<<endl;
cout<<"逆序输出2边:"<<endl;
for(int j=0;j<(i+1)*2;j++)
{
cout<<p->data<<" ";
p=p->pre;
}
}
int main()
{
Node *r;
r=CircularLinkList_Create(r,10);
PrintTwiceElem(r,10);
return 0;
}
四、静态链表
静态链表是个结构体数值但,里面存两个值,一个data值另一个是下一个数组的下标,没什么难的
#define MaxSize 10
typedef int ElemType;
struct Node
{
ElemType data;
int index;
};
Node n[MaxSize];