📄 循环表的建立及删除头结点.cpp
字号:
#include<iostream.h>
#include"stdlib.h"//malloc函数需要包含的头文件
struct nodetype
{
int data;
nodetype *next;
}typedef Linklist;
nodetype *create()
{
int d;
nodetype *h=NULL,*s,*t=NULL,*p;
int i=1;
cout<<"建立一个循环单链表(0表示输入结束):"<<endl;
while(1)
{
cout<<"输入第"<<i<<"结点的值:";
cin>>d;
if(d==0)break;//以0表示输入结束
if(i==1)//建立第一个结点
{
h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
{
s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;//t始终指向最后一个结点
}
i++;
}
if(t!=NULL) t->next=h;
p=h;
cout<<"创建的链表为:"<<endl;
while(p->next!=h)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<p->data<<" "<<endl;
return h;
}
void num(nodetype *h)
{
int i=1;
nodetype *p=h;
while(p->next!=h)
{
p=p->next;i++;
}
cout<<"链表中结点的个数是:"<<i<<endl;
}
nodetype *del(nodetype *h,int i)
{
nodetype *p=h,*r;
if(i==1)
{
r=h;
while(r->next!=h) r=r->next;
h=h->next;r->next=h;
free(p);
}
p=h;
cout<<"删除头节点后的链表为:"<<endl;
while(p->next!=h)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<p->data<<" "<<endl;
return h;
}
void main()
{
nodetype *lianbiao;
lianbiao=create();
num(lianbiao);
del(lianbiao,1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -