模板 --双链表
struct node{
int data;
struct node *next;
struct node *prior; //多一个前指针prior
};
void initList(node *&head){
head=new node();
head->next=NULL;
head->prior=NULL;
}
void destoryList(node *&head){ //销毁链表
node *pre=head;
node *p=head->next; //从头结点开始删除
while(p!=NULL){
delete pre; //c语言:free(pre);
pre=p;
p=p->next;
}
delete pre;
}
int getLength(node *head){ //求长度
int count=0;
node *p=head->next; //工作指针
while(p!=NULL){
p=p->next;
count++;
}
return count;
}
void insElem(node *&head,int value,int i){ //在第i个元素后插入元素,值为value
node *p=head;
int count=1;
while (p!=NULL&&count!=i) //找到第i-1个结点
count++,p=p->next;
node *q=new node ();
q->data=value;
q->next=p->next;//1
if(p->next!=NULL)//判断p是不是尾结点
p->next->prior=q;//2
q->prior=p;//3
p->next=q;//4
}
void output(node *head)
{
node *p=head->next;
while (p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void deleElem(node *&head,int i){ //删除第i个元素
int count=0;
node *p=head;
while(p!=NULL&&count!=i) //直接找第i个,改变它的指针域
count++,p=p->next;
node *pre=new node ();
pre=p->prior;//保存前驱节点
if(p->next!=NULL)
p->next->prior=pre;//1 绕过自己
pre->next=p->next;//2
delete p;
}
void createList_Tail(node *&head,int a[],int len){
node *tc=head;
for(int i=0;i<len;i++){
node *s=new node ();
s->data=a[i];
tc->next=s;
s->prior=tc;
tc=s;
}
tc->next=NULL;
}
模板–循环双链表
#include <iostream>
#define Element char
using namespace std;
struct node{
Element data;
node *next;
node *prior;
};
void initList(node *&head){ //初始化双链表
head=new node();
head->next=head;
head->prior=head;
}
void create_Tail(node *&head,Element a[],int len){ //尾插法建表
node *tc=head;
for(int i=0;i<len;i++){
node *s=new node();
s->data=a[i];
tc->next=s;
s->prior=tc;
tc=s;
}
tc->next=head; //最后一个结点的prior应该指向head
}
void output(node *head){ //输出
node *p=head->next;
while (p!=head)
cout<<p->data<<" ",p=p->next;
cout<<endl;
}
int getLength(node *head){ //求长度
int count=0;
node *p=head->next;
while (p!=head)
count++,p=p->next;
return count;
}
bool isEmpty(node *head){ //判断是否为空
return head->next==head; //1为空
}
Element findTh(node *head,int th){ //找到第th个结点
node *p=head->next;
int count=1;
while (p!=head&&count!=th)
count++,p=p->next;
return p->data;
}
int findValue(node *head,Element value){ //找到值为value的结点
int count=1;
node *p=head->next;
while (p!=head&&p->data!=value)
count++,p=p->next;
return count;
}
void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点
node *p=head->next;
int count=1;
while (p!=head&&count<th)
count++,p=p->next;
node *s=new node();
s->data=e;
s->next=p->next;
p->next->prior=s;
s->prior=p;
p->next=s;
}
void delElem(node *&head,int th){ //删除第th个结点
node *p=head->next;
int count=1;
while (p!=head&&count!=th)
count++,p=p->next;
p->next->prior=p->prior;
p->prior->next=p->next;
delete p;
}
void destoryList(node *&head){ //释放双链表
node *pre=head,*p=pre->next;
while (p!=head){
delete pre;
pre=p;
p=p->next;
}
delete pre;
}
int main()
{
node *head;
initList(head);
int n;
char a[1000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
create_Tail(head,a,n);
output(head);
cout<<getLength(head)<<endl;
if(isEmpty(head))cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout<<findTh(head,3)<<endl;
cout<<findValue(head,'a')<<endl;
insElem(head,'f',3);
output(head);
delElem(head,5);
output(head);
destoryList(head);
return 0;
}
1.双链表的基本运算
实现循环单链表的基本运算:初始化、插入、删除、求表的长度、判空、释放。
(1)初始化循环单链表L,输出(L->next==L)的逻辑值;
(2)依次采用尾插法插入元素:输入分两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。
(3)输出循环单链表L;
(4)输出循环单链表L的长度;
(5)判断循环单链表L是否为空;
(6)输出循环单链表L的第3个元素;
(7)输出元素a的位置;
(8)在第4个元素位置上插入‘w’元素;
(9)输出循环单链表L;
(10)删除L的第5个元素;
(11)输出循环单链表L;
(12)释放循环单链表L。
#include <iostream>
using namespace std;
#define Element char
struct node{
Element data;
node *next;
node *prior;
};
void initList(node *&head){ //初始化双链表
head=new node();
head->next=NULL;
head->prior=NULL;
}
void create_Tail(node *&head,Element a[],int len){ //尾插法建表
node *tc=head;
for(int i=0;i<len;i++){
node *s=new node();
s->data=a[i];
tc->next=s;
s->prior=tc;
tc=s;
}
tc->next=NULL;
}
void output(node *head){ //输出
node *p=head->next;
while (p!=NULL)
cout<<p->data<<" ",p=p->next;
cout<<endl;
}
int getLength(node *head){ //求长度
int count=0;
node *p=head->next;
while (p!=NULL)
count++,p=p->next;
return count;
}
bool isEmpty(node *head){ //判断是否为空
return head->next==NULL; //1为空
}
Element findTh(node *head,int th){ //找到第th个结点
node *p=head->next;
int count=1;
while (p!=NULL&&count!=th)
count++,p=p->next;
return p->data;
}
int findValue(node *head,Element value){ //找到值为value的结点
int count=1;
node *p=head->next;
while (p!=NULL&&p->data!=value)
count++,p=p->next;
return count;
}
void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点
node *p=head;
int count=0;
while (p!=NULL&&count!=th)
count++,p=p->next;
node *s=new node();
s->data=e;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
}
void delElem(node *&head,int th){ //删除第th个结点
node *p=head->next;
int count=1;
while (p!=NULL&&count!=th)
count++,p=p->next;
p->next->prior=p->prior;
p->prior->next=p->next;
delete p;
}
void destoryList(node *&head){ //释放双链表
node *pre=head,*p=pre->next;
while (p!=NULL){
delete pre;
pre=p;
p=p->next;
}
delete pre;
}
int main()
{
node *head;
initList(head);
int n;
char a[1000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
create_Tail(head,a,n);
output(head);
cout<<getLength(head)<<endl;
if(isEmpty(head))cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout<<findTh(head,3)<<endl;
cout<<findValue(head,'a')<<endl;
insElem(head,'f',3);
output(head);
delElem(head,5);
output(head);
destoryList(head);
return 0;
}
2:整数双链表的基本运算-3
题目链接:传送门
void PDDoutput(node *head){ //反向输出
node *p=head->next;
while (p->next!=NULL) //找到最后一个结点
p=p->next;
while (p!=head)
cout<<p->data<<" ",p=p->prior; //借助prior往前
cout<<endl;
}
3:整数双链表的基本运算-4
借助一个计数数组来记录各数字出现的次数,然后将出现次数恰好为1的数字连接到头结点后
int num[1000]={}; //计数数组
void simplify(node *&head){
node *p=head->next;
while (p->next!=NULL){ //保证停止的时候p指向最后一个结点
num[p->data]++; //记录p->data出现了一次
p=p->next;
}
num[p->data]++; //记录最后一个结点的数据
while(p!=head){ //逆向删除,以保证被删掉的是“后面的”数据
num[p->data]--; //次数减小
if(num[p->data]!=0){ //如果不为0,说明前面还有一样的数,那么删除现在这个结点
if(p->next!=NULL) //删除时候注意判断边界
p->next->prior=p->prior;
if(p->prior!=NULL)
p->prior->next=p->next;
delete p;
}
p=p->prior; //逆向查找
}
}
4:循环双链表的基本运算
#include <iostream>
#define Element char
using namespace std;
struct node{
Element data;
node *next;
node *prior;
};
void initList(node *&head){ //初始化双链表
head=new node();
head->next=head;
head->prior=head;
}
void create_Tail(node *&head,Element a[],int len){ //尾插法建表
node *tc=head;
for(int i=0;i<len;i++){
node *s=new node();
s->data=a[i];
tc->next=s;
s->prior=tc;
tc=s;
}
tc->next=head; //最后一个结点的prior应该指向head
}
void output(node *head){ //输出
node *p=head->next;
while (p!=head)
cout<<p->data<<" ",p=p->next;
cout<<endl;
}
int getLength(node *head){ //求长度
int count=0;
node *p=head->next;
while (p!=head)
count++,p=p->next;
return count;
}
bool isEmpty(node *head){ //判断是否为空
return head->next==head; //1为空
}
Element findTh(node *head,int th){ //找到第th个结点
node *p=head->next;
int count=1;
while (p!=head&&count!=th)
count++,p=p->next;
return p->data;
}
int findValue(node *head,Element value){ //找到值为value的结点
int count=1;
node *p=head->next;
while (p!=head&&p->data!=value)
count++,p=p->next;
return count;
}
void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点
node *p=head->next;
int count=1;
while (p!=head&&count<th)
count++,p=p->next;
node *s=new node();
s->data=e;
s->next=p->next;
p->next->prior=s;
s->prior=p;
p->next=s;
}
void delElem(node *&head,int th){ //删除第th个结点
node *p=head->next;
int count=1;
while (p!=head&&count!=th)
count++,p=p->next;
p->next->prior=p->prior;
p->prior->next=p->next;
delete p;
}
void destoryList(node *&head){ //释放双链表
node *pre=head,*p=pre->next;
while (p!=head){
delete pre;
pre=p;
p=p->next;
}
delete pre;
}
int main()
{
node *head;
initList(head);
int n;
char a[1000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
create_Tail(head,a,n);
output(head);
cout<<getLength(head)<<endl;
if(isEmpty(head))cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout<<findTh(head,3)<<endl;
cout<<findValue(head,'a')<<endl;
insElem(head,'f',3);
output(head);
delElem(head,5);
output(head);
destoryList(head);
return 0;
}