#include"stdio.h"
#include"malloc.h"
#define NULL 0
struct node
{
char name[20];
struct node *next,*prior;
};
struct node *creat(int n)
{
struct node *h,*p,*s;
int i;
h=(struct node*)malloc(sizeof(struct node));
h->name[0]='\0';
h->prior=NULL;
h->next=NULL;
p=h;
for(i=0;i<n;i++)
{
s=(struct node*)malloc(sizeof(struct node));
p->next=s;
printf("Input the %d student's name:",i+1);
scanf("%s",s->name);
s->prior=p;
s->next=NULL;
p=s;
}
p->next=NULL;
return h;
}
struct node *search(struct node *h,char*x)
{
struct node *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 datd!\n");
}
void del(struct node *p)
{
p->next->prior=p->prior;
p->prior->next=p->next;
free(p);
}
main()
{
int number;
char sname[20];
struct node *head,*sp;
puts("Please in put the size of the list;");
scanf("%d",&number);
head=creat(number);
sp=head->next;
printf("\nNow the double list is:\n");
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 to find is: %s\n",*&sp->name);
del(sp);
sp=head->next;
printf("\nNow the double list is:\n");
while(sp)
{
printf(" %s",&*(sp->name));
sp=sp->next;
}
printf("\n");
puts("\n Press any key to quit...");
getch();
}求人解释我备注了的地方
c语言双向链
- 提问者网友:人生佛魔见
- 2021-05-04 21:50
- 五星知识达人网友:痴妹与他
- 2021-05-04 23:04
#include"stdio.h"
#include"malloc.h"
#define NULL 0
struct node
{
char name[20];
struct node *next,*prior; //定义结构结点指针
};
struct node *creat(int n)
{
struct node *h,*p,*s;
int i;
h=(struct node*)malloc(sizeof(struct node));
h->name[0]='\0';
h->prior=NULL;
h->next=NULL;
p=h;
for(i=0;i<n;i++)
{
s=(struct node*)malloc(sizeof(struct node));
p->next=s; // 让指针p所指结点指向s结点,即h结点的下一结点是s
printf("Input the %d student's name:",i+1);
scanf("%s",s->name);
s->prior=p;
s->next=NULL; //s指向空节点,即s是尾结点
p=s; //让指针p指向向结点s,此时指针p所指结点编出s
}
p->next=NULL;
return h;
}
struct node *search(struct node *h,char*x)
{
struct node *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 datd!\n");
}
void del(struct node *p)
{
p->next->prior=p->prior;//指针p所指结点的下一结点的头结点,指向指针p所指结点的前一节点
p->prior->next=p->next; // 指针p所指结点的前一节点指向,指针p所指结点的后一节点
//这两步实现将p所指结点,排除到双向链表之外
free(p); //删除p所指结点
}
main()
{
int number;
char sname[20];
struct node *head,*sp;
puts("Please in put the size of the list;");
scanf("%d",&number);
head=creat(number);
sp=head->next;
printf("\nNow the double list is:\n");
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 to find is: %s\n",*&sp->name);
del(sp);
sp=head->next;
printf("\nNow the double list is:\n");
while(sp)
{
printf(" %s",&*(sp->name));
sp=sp->next;
}
printf("\n");
puts("\n Press any key to quit...");
getch();
}
- 1楼网友:由着我着迷
- 2021-05-05 00:15
h->prior=NULL; 是的
s->prior=p; 保证每次建立链表的表尾都是空
p->next->prior=p->prior;
p->prior->next=p->next;
free(p);
就是跳过p 把p之前的结点与之后的结点链接起来 然后安全的删除结点p