#include <stdio.h>
#include <stdlib.h>
//用户自定义双向链表
typedef struct node
{
int num;
struct node *prior;//前指针
struct node *next;//后指针
}stud;
//指针函数
stud* create(int n)
{
stud *p,*h,*s;
int i;
h=(stud*)malloc(sizeof(stud));
h->num=0;
h->next=h->prior=NULL;
p=h;
for(i=0;i<n;i++)
{
s=(stud*)malloc(sizeof(stud));
p->next=s;
s->num=rand()%100;
s->prior=p;
s->next=NULL;
p=s;
}
p->next=NULL;
return h;
}
stud* search(stud *h,int NumOfSearch)
{
stud *p;
p=h->next;
while(p)
{
if(p->num==NumOfSearch)
return p;
else
p=p->next;
}
printf("没有这个数据");
return NULL;
}
void del(stud *p)
{
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
}
stud* insert(stud *h,int NumOfInsert,int NumOfAddr)
{
stud *p,*Insertstruct;
int i=1;//人类的正常思维1-2-3-4
p=h->next;
while(p){
if(i==NumOfAddr-1)//因为当执行这句语句时i==NumOfAddr那么就会在需求插入的后一位插入
break;
else{
i++;
p=p->next;
}
}
Insertstruct=(stud*)malloc(sizeof(stud));
Insertstruct->num=NumOfInsert;
Insertstruct->next=p->next;
p->next=Insertstruct;
}
stud* display(stud *head)
{
stud *p;
p=head->next;
while(p)
{
printf("%d\t",p->num);
p=p->next;
}
printf("\n");
}
int main(void)
{
stud *head,*NumOfDel;
head=create(10);//创建双向链表
display(head);
NumOfDel=search(head,0);//查询数字为0的指针顺序
del(NumOfDel);//删除
display(head);
insert(head,10,3);//在链表第三位插入数字10
display(head);
}
接下来是执行效果