数据结构-【链表】单向链表的逆置和双向循环链表

单向链表的逆置

1、为什么不能用auto变量的地址去初始化static型指针?
因为Static是在编译时进行初始化的,而Auto是在运行时初始化的,在编译时,Auto变量还不存在呢,当然也就没有地址,初始化就不能成功了.
#include 
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结束:"<>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结束:"<>id;
	}
}

void reverse_list(Stu **head)//翻转链表
{
	Stu *pp=NULL;
	Stu *pNewHead=NULL;
	if(!(*head))
	{
		cout<<"the list is NULL!"<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<number<<" ";
		p=p->pNext;
	}
	cout<pNext;
		delete temp;
	}
}

int main()
{
	Stu *head=NULL;
	create_list(&head);
	show_list(head);
	cout<
运行结果:
数据结构-【链表】单向链表的逆置和双向循环链表_第1张图片
双向循环链表
实现代码如下:
#include 
#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<<"请输入学生的学号:"<>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号,以结束:"<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<<"从头遍历:"<number;
		temp=temp->next;
	}while(temp!=phead);
	cout<number;
		temp=temp->pre;
	}while(temp!=pend);
	cout<pre=NULL;
	ps->next=NULL;
	cout<<"请输入你要插入的学号:"<>ps->number;
	if(!phead)
	{
		phead=ps;
		cout<<"链表为空,插入成功!"<number > ps->number)//插入的数据在表头;
		{
			ps->next=phead;
			ps->pre=phead->pre;
			pend->next=ps;
			phead->pre=ps;
			phead=ps;
			cout<<"插入在表头!"<next!=phead)
			{
				if(bj->next->number > ps->number) //在表的中间插入;
				{
					ps->next=bj->next;
					bj->next->pre=ps;
					ps->pre=bj;
					bj->next=ps;
					cout<<"成功插入在链表的中间!"<next;
			}
		}
	}
}
void Delete_List()
{
	stu *temp=NULL;
	stu *bj=NULL;
	int id;
	cout<<"请输入你要删除的学号:"<>id;
	if(!phead)
	{
		cout<<"the list is null!"<number==id)
		{
			temp=phead;
			phead=phead->next;
			phead->pre=pend;
			pend->next=phead;
			delete temp;
			cout<<"成功删除在头结点"<next != phead)
			{
				if(bj->next->number==id)
				{
					temp=bj->next;
					temp->next->pre=bj;
					bj->next=temp->next;
					delete temp;
					cout<<"成功删除在链表的中间位置!"<>id;
	if(!phead)
	{
		cout<<"the list is null!"<number==id)
		{
			cout<<"查找成功,位置在表头!"<number<next!=phead)
			{
				if(bj->next->number == id)
				{
					cout<<"查找成功,位置在表中!"<next->number<next;
			}
		}
	}
	cout<<"该信息不存在!"<>oldid>>newid;
	if(!phead)
	{
		cout<<"the list is null!"<number==oldid)
		{
			phead->number=newid;
			cout<<"学号修改成功,在表头!"<next->number==oldid)
			{
				bj->next->number=newid;
				cout<<"学号修改成功,在表中!"<>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<<"你正确输入你要执行的操作:"<

运行结果:

数据结构-【链表】单向链表的逆置和双向循环链表_第2张图片

你可能感兴趣的:(C,基础,数据结构与算法)