双向链表的创建删除

//双向链表
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct node
{
 char name[20];
 struct node *prior,*next;
}stud;

//创建双向链表
stud *creat(int n)
{
 stud *p,*h,*s;
 int i;
 h=(stud*)malloc(sizeof(stud));
 h->name[0]='\0';
 h->next=NULL;
 h->prior=NULL;
 p=h;
 for(i=0;i<n;i++)
 {
  s=(stud*)malloc(sizeof(stud));
  p->next=s;
  printf("input the %d students' name", i+1);
  scanf("%s",s->name);
  s->prior=p;
  s->next=NULL;
  p=s;
 }
 p->next=NULL;
 return(h);
}
//查找
stud*search(stud*h,char*x)
{
 stud*p;
 char*y;
 p=h->next;
 while(p)
 {
  y=p->name;
  if(strcmp(y,x)==0)
  {
   return(p);
  }else
  {
   p=p->next;
  }
  printf("cannot find data\n");
 }
 return NULL;
}

//删除
void del(stud*p)
{
 p->next->prior=p->prior;
 p->prior->next=p->next;
 free(p);
}

void main()
{
int number;
char sname[20];
stud *head,*sp;
puts("Please input the size of the list");
scanf("%d",&number);
head=creat(number);
sp=head->next;
sp=head->next;
while(sp)
{
	printf("%s",sp->name);
    sp=sp->next;
}
printf("\nPlease input the name which you want to find:\n");
scanf("%s",sname);
sp=search(head,sname);
printf("the name you want find is :%s\n",sp->name);
del(sp);
sp=head->next;
printf("Now the double list is :\n");
while(sp)
{

 printf("%s",&*(sp->name));
 sp=sp->next;

}
printf("\n");
puts("\n Press any key to quit..");


}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值