版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40667484/article/details/79515514
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *Init(struct node *head);
void Print_list(struct node *head);
struct node *insert(struct node *head,int i);
struct node *Delete(struct node *head,int i);
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *Init(struct node *head);
void Print_list(struct node *head);
struct node *insert(struct node *head,int i);
struct node *Delete(struct node *head,int i);
struct node *Delete(struct node *head,int i)
{
struct node *t,*s;
struct node *pr=head;
int j;
if(i==1)
{
for(t=pr;t->next!=pr;t=t->next); //找尾指针 //找到最后一个节点
s=pr; //删除第一个节点意味着连接第二个节点
pr=pr->next; //找到指向第二个节点的指针
t->next=pr;
free(s);
return pr; //删除第一个节点
}
else
{
t=pr;
for(j=1;j<i;j++) //找到要删除的位置的前一个指针
{ //连接删除位置后一个节点
t=t->next;
}
s=t->next;
t->next=s->next;
return pr;
}
}
{
struct node *t,*s;
struct node *pr=head;
int j;
if(i==1)
{
for(t=pr;t->next!=pr;t=t->next); //找尾指针 //找到最后一个节点
s=pr; //删除第一个节点意味着连接第二个节点
pr=pr->next; //找到指向第二个节点的指针
t->next=pr;
free(s);
return pr; //删除第一个节点
}
else
{
t=pr;
for(j=1;j<i;j++) //找到要删除的位置的前一个指针
{ //连接删除位置后一个节点
t=t->next;
}
s=t->next;
t->next=s->next;
return pr;
}
}
struct node *insert(struct node *head,int i)
{
int item;
int j;
struct node *s,*t;
struct node *pr=head; //第一步:用一个指针pr代替传入的head指针 用pr来操作链表
//第二步:判断传入的链表是否为空
//第三步:为空 结束 无法插入;不为空,插入(循环的插入逻辑很简单)
printf("输入要插入的值\n");
scanf("%d",&item);
if(i==1)
{
s=(struct node *)malloc(sizeof(struct node)); //创建一个节点s
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=pr->next;
pr->next=s;
return pr;
}
else
{
t=pr;
for(j=1;j<i;j++)
{
t=t->next; //遍历到要插入的位置,一般来说用循环次数来结束遍历到了第几个位置
}
s=(struct node *)malloc(sizeof(struct node));
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=t->next;
t->next=s;
return pr;
}
}
void Print_list(struct node *head)
{
struct node *p;
for(p=head->next;p!=head;p=p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
{
int item;
int j;
struct node *s,*t;
struct node *pr=head; //第一步:用一个指针pr代替传入的head指针 用pr来操作链表
//第二步:判断传入的链表是否为空
//第三步:为空 结束 无法插入;不为空,插入(循环的插入逻辑很简单)
printf("输入要插入的值\n");
scanf("%d",&item);
if(i==1)
{
s=(struct node *)malloc(sizeof(struct node)); //创建一个节点s
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=pr->next;
pr->next=s;
return pr;
}
else
{
t=pr;
for(j=1;j<i;j++)
{
t=t->next; //遍历到要插入的位置,一般来说用循环次数来结束遍历到了第几个位置
}
s=(struct node *)malloc(sizeof(struct node));
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=t->next;
t->next=s;
return pr;
}
}
void Print_list(struct node *head)
{
struct node *p;
for(p=head->next;p!=head;p=p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
struct node *Init(struct node *head)
{
int item;
struct node *t;
struct node *s;
struct node *pr=head; //第一步:用一个指针pr代替传入的head指针 用pr来操作链表
//第二步:判断传入的链表是否为空
//第三步:为空 一种初始化方式;不为空,另一种初始化方式(循环的创建逻辑很简单)
while(1)
{
printf("输入节点的值\n");
scanf("%d",&item);
fflush(stdin);
if(pr==NULL) //判断为空表之后的操作
{
pr=(struct node *)malloc(sizeof(struct node)); //创建一个头结点,然后使头尾相接
if(pr==NULL)
{
printf("创建失败\n");
exit(0);
}
pr->next=pr; //头尾相接的操作
return pr;
}
else //如果不是空表 之后的操作 不是空表,自带头结点
{
for(t=pr;t->next!=pr;t=t->next); //用个循环找到尾结点,后面需要用尾结点操作
s=(struct node *)malloc(sizeof(struct node)); //创建一个节点 插入到头节点和尾结点之间
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=pr;
t->next=s; //将s新节点插入到头结点和尾节点之间的操作
return pr;
}
}
}
int main()
{
struct node *head=NULL;
int n,i;
while(n!=0)
{
printf("单循环链表的基本操作\n");
printf("-----------------------------");
printf("1.创建一个节点\n");
printf("2.插入节点\n");
printf("3.删除节点\n");
printf("4.显示链表成员\n");
printf("0.退出\n");
printf("请选择操作\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
head=Init(head);
break;
}
case 2:
{
printf("插入的位置是\n");
scanf("%d",&i);
head=insert(head,i);
break;
}
case 3:
{
printf("删除的位置是\n");
scanf("%d",&i);
head=Delete(head,i);
break;
}
case 4:
{
Print_list(head);
break;
}
}
}
return 0;
}
{
int item;
struct node *t;
struct node *s;
struct node *pr=head; //第一步:用一个指针pr代替传入的head指针 用pr来操作链表
//第二步:判断传入的链表是否为空
//第三步:为空 一种初始化方式;不为空,另一种初始化方式(循环的创建逻辑很简单)
while(1)
{
printf("输入节点的值\n");
scanf("%d",&item);
fflush(stdin);
if(pr==NULL) //判断为空表之后的操作
{
pr=(struct node *)malloc(sizeof(struct node)); //创建一个头结点,然后使头尾相接
if(pr==NULL)
{
printf("创建失败\n");
exit(0);
}
pr->next=pr; //头尾相接的操作
return pr;
}
else //如果不是空表 之后的操作 不是空表,自带头结点
{
for(t=pr;t->next!=pr;t=t->next); //用个循环找到尾结点,后面需要用尾结点操作
s=(struct node *)malloc(sizeof(struct node)); //创建一个节点 插入到头节点和尾结点之间
if(s==NULL)
{
printf("创建失败\n");
exit(0);
}
s->data=item;
s->next=pr;
t->next=s; //将s新节点插入到头结点和尾节点之间的操作
return pr;
}
}
}
int main()
{
struct node *head=NULL;
int n,i;
while(n!=0)
{
printf("单循环链表的基本操作\n");
printf("-----------------------------");
printf("1.创建一个节点\n");
printf("2.插入节点\n");
printf("3.删除节点\n");
printf("4.显示链表成员\n");
printf("0.退出\n");
printf("请选择操作\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
head=Init(head);
break;
}
case 2:
{
printf("插入的位置是\n");
scanf("%d",&i);
head=insert(head,i);
break;
}
case 3:
{
printf("删除的位置是\n");
scanf("%d",&i);
head=Delete(head,i);
break;
}
case 4:
{
Print_list(head);
break;
}
}
}
return 0;
}