/*
* 单循环链表的基本操作
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
int main()
{
Node *creat();
Node *insert(Node *head);
Node *insert2(Node *head);
Node *del(Node *head);
Node *show(Node *head);
int surface();
char a;
Node *pt;
while(1){
surface();
scanf("%c",&a);
switch(a){
case '1':{pt=creat();break;}
case '2':{pt=insert(pt);break;}
case '3':{pt=insert2(pt);break;}
case '4':{pt=del(pt);break;}
case '5':{pt=show(pt);break;}
case '0':{return;}
}
}
}
//创建空链表
Node *creat()
{
setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
Node *head;
head = (Node*)malloc(sizeof(Node));
head->next = head;
printf("链表初始化完成!!任意健继续!");
getchar();
return head;
}
//插入节点(尾插法)
Node *insert(Node *head)
{
Node *p,*q;
//使q指向最后一个节点
for(q=head;q->next!=head;q=q->next);
while(1)
{
//使指针p指向新开辟节点,并对其数据域赋值
p = (Node*)malloc(sizeof(Node));
printf("输入新插入学生成绩");
scanf("%d",&p->data);
if(p->data==0){
break;
}
p->next = head;
q->next = p;
q = p;
}
setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
printf("节点插入完成!!任意健继续!");
getchar();
return head;
}
//插入节点(头插法)
Node *insert2(Node *head)
{
Node *p,*q;
while(1)
{
//使指针p指向新开辟节点,并对其数据域赋值
p = (Node*)malloc(sizeof(Node));
printf("输入新插入学生成绩");
scanf("%d",&p->data);
if(p->data==0){
break;
}
p->next = head->next;
head->next = p;
}
setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
printf("节点插入完成!!任意健继续!");
getchar();
return head;
}
//删除节点
Node *del(Node *head)
{
Node *p,*q;
int a;
printf("输入要删除的成绩");
scanf("%d",&a);
for(p = head;p->next!=NULL;p=p->next){
//寻找前趋节点
if(p->next->data==a){
break;
}
}
if (p->next == NULL) return head;
q = p->next;
p->next = p->next->next;
free(q);
printf("节点删除完成!!任意健继续!");
setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
getchar();
return head;
}
//输出所有节点数据
Node *show(Node *head)
{
Node *p;
for(p=head->next;p!=head;p=p->next){
printf("%4d",p->data);
}
printf("\n按任意健继续!");
getchar();
getchar();
return head;
}
//主界面
int surface()
{
system("clear");
printf("**单循环链表的基本操作**\n");
printf("1.初始化空链表\n");
printf("2.插入节点(尾插法)\n");
printf("3.插入节点(头插法)\n");
printf("4.删除节点\n");
printf("5.打印所有数据\n");
printf("0.退出系统\n");
printf("输入选项:");
}
单循环链表的基本操作
猜你喜欢
转载自blog.csdn.net/zuiziyoudexiao/article/details/80373637
今日推荐
周排行