单向链表的逆置
1、为什么不能用auto变量的地址去初始化static型指针?因为Static是在编译时进行初始化的,而Auto是在运行时初始化的,在编译时,Auto变量还不存在呢,当然也就没有地址,初始化就不能成功了.
#include <iostream>
using namespace std;
typedef struct student
{
int number;
struct student *pNext;
} Stu;
void create_list(Stu **head) //创建链表;
{
Stu *pend=NULL;
int id;
cout<<"请输入学生的ID,以0结束:"<<endl;
cin>>id;
while(id != 0)
{
Stu *ps=new Stu;
ps->pNext=NULL;
ps->number=id;
if(!(*head))
{
*head=ps;
pend=ps;
}
else
{
pend->pNext=ps;
pend=ps;
}
cout<<"请输入学号ID,以0结束:"<<endl;
cin>>id;
}
}
void reverse_list(Stu **head)//翻转链表
{
Stu *pp=NULL;
Stu *pNewHead=NULL;
if(!(*head))
{
cout<<"the list is NULL!"<<endl;
return ;
}
while(*head)
{
pp=*head;
(*head)=(*head)->pNext;
if(!pNewHead)
{
pNewHead=pp;
pp->pNext=NULL;//一下把所有的链表的后面都赋空值了;
}
else
{
pp->pNext=pNewHead;
pNewHead=pp;
}
}
*head=pNewHead;
}
void show_list(Stu *head)
{
Stu *p=head;
while(p)
{
cout<<p->number<<" ";
p=p->pNext;
}
cout<<endl;
}
void DestoryList(student *head) //释放内存;
{
student *temp=NULL;
while(head)
{
temp=head;
head=head->pNext;
delete temp;
}
}
int main()
{
Stu *head=NULL;
create_list(&head);
show_list(head);
cout<<endl;
reverse_list(&head);//逆置链表
show_list(head);
delete head;
system("pause");
return 0;
}
运行结果:
双向循环链表
实现代码如下:
#include <iostream>
#include "windows.h"
using namespace std;
typedef struct student
{
int number;
struct student *pre;
struct student *next;
}stu;
stu *pend=NULL;
stu *phead=NULL;
void Create_List()//创建链表;
{
stu *ps=NULL;
ps=new stu;
ps->next=NULL;
ps->pre=NULL;
cout<<"请输入学生的学号:"<<endl;
cin>>ps->number;
while(ps->number!=0)
{
if(!phead)//空表的插入;
{
phead=ps;
pend=ps;
}
else
{
pend->next=ps;
ps->pre=pend;
pend=ps;
}
ps=new stu;
cout<<"请输入学生的ID号,以结束:"<<endl;
ps->next=NULL;
ps->pre=NULL;
cin>>ps->number;
}
delete ps;
pend->next=phead;//连接表头表尾;
phead->pre=pend;
}
void Show_List() //从两个方向遍历;
{
stu *temp=NULL; //正向遍历;
temp=phead;
cout<<"从头遍历:"<<endl;
do //do{} while (); 先执行在判断,所以刚好可以解决双向链表的打印问题;
{
if(!temp)
{
cout<<"the list is NULL!"<<endl;
return ;
}
cout<<temp->number;
temp=temp->next;
}while(temp!=phead);
cout<<endl;
temp=pend; //反向遍历;
cout<<"从尾遍历:"<<endl;
do
{
if(!temp)
{
cout<<"the list is NULL!"<<endl;
return ;
}
cout<<temp->number;
temp=temp->pre;
}while(temp!=pend);
cout<<endl;
}
void Insert_List() //插入数据;
{
stu *ps=new stu;
stu *temp=NULL;
stu *bj=NULL;
ps->pre=NULL;
ps->next=NULL;
cout<<"请输入你要插入的学号:"<<endl;
cin>>ps->number;
if(!phead)
{
phead=ps;
cout<<"链表为空,插入成功!"<<endl;
return ;
}
else
{
if(phead->number > ps->number)//插入的数据在表头;
{
ps->next=phead;
ps->pre=phead->pre;
pend->next=ps;
phead->pre=ps;
phead=ps;
cout<<"插入在表头!"<<endl;
return;
}
else
{
bj=phead;
while(bj->next!=phead)
{
if(bj->next->number > ps->number) //在表的中间插入;
{
ps->next=bj->next;
bj->next->pre=ps;
ps->pre=bj;
bj->next=ps;
cout<<"成功插入在链表的中间!"<<endl;
return ;
}
bj=bj->next;
}
}
}
}
void Delete_List()
{
stu *temp=NULL;
stu *bj=NULL;
int id;
cout<<"请输入你要删除的学号:"<<endl;
cin>>id;
if(!phead)
{
cout<<"the list is null!"<<endl;
return;
}
else
{
if(phead->number==id)
{
temp=phead;
phead=phead->next;
phead->pre=pend;
pend->next=phead;
delete temp;
cout<<"成功删除在头结点"<<endl;
return;
}
else
{
bj=phead;
while(bj->next != phead)
{
if(bj->next->number==id)
{
temp=bj->next;
temp->next->pre=bj;
bj->next=temp->next;
delete temp;
cout<<"成功删除在链表的中间位置!"<<endl;
return;
}
}
}
}
}
void Search_List()
{
int id;
stu *temp=NULL;
stu *bj=NULL;
cout<<"请输入你要查询的学生的学号:"<<endl;
cin>>id;
if(!phead)
{
cout<<"the list is null!"<<endl;
return;
}
else
{
if(phead->number==id)
{
cout<<"查找成功,位置在表头!"<<endl;
cout<<phead->number<<endl;
return;
}
else
{
bj=phead;
while(bj->next!=phead)
{
if(bj->next->number == id)
{
cout<<"查找成功,位置在表中!"<<endl;
cout<<bj->next->number<<endl;
return;
}
bj=bj->next;
}
}
}
cout<<"该信息不存在!"<<endl;
}
void Update_List()
{
stu *bj=new stu;
int oldid,newid;
cout<<"请输入你要修改的学号和新的学号:"<<endl;
cin>>oldid>>newid;
if(!phead)
{
cout<<"the list is null!"<<endl;
return ;
}
else
{
if(phead->number==oldid)
{
phead->number=newid;
cout<<"学号修改成功,在表头!"<<endl;
return;
}
else
{
bj=phead;
while(bj->next->number==oldid)
{
bj->next->number=newid;
cout<<"学号修改成功,在表中!"<<endl;
return ;
}
}
}
cout<<"该信息不存在!"<<endl;
}
void menue()
{
while(1)
{
int choice=0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
//设置代码输出的颜色代码,green,red,blue;
cout<<"请选择你要执行的操作:"<<endl<<endl;
cout<<"1.创建链表:"<<endl;
cout<<"2.插入数据:"<<endl;
cout<<"3.查询数据:"<<endl;
cout<<"4.更新数据:"<<endl;
cout<<"5.删除数据:"<<endl;
cout<<"6.显示数据:"<<endl;
cout<<"7.退出:"<<endl<<endl;
cin>>choice;
switch(choice)
{
case 1:Create_List();
break;
case 2:Insert_List();
break;
case 3:Search_List();
break;
case 4:Update_List();
break;
case 5:Delete_List();
break;
case 6:Show_List();
break;
case 7:
return ;
default:
cout<<"你正确输入你要执行的操作:"<<endl;
}
}
}
int main()
{
menue();
system("pause");
return 0;
}
运行结果: