#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int date ;
struct Node *qu;
struct Node *hj;
}Node;
typedef Node* Linklist;
void chuangjian (Linklist* L,int i)
{
Linklist p;
*L = (Linklist)malloc(sizeof(Node));
(*L)->qu=NULL;
(*L)->hj=NULL;
int j=1;
while (j<=i) {
p=(Linklist)malloc(sizeof(Node));//linklist
scanf("%d",&(p->date));
p->hj=(*L)->hj;
(*L)->hj=p;
p->qu=(*L);
j++;
}
}
void chazhao(Linklist L,int i,int *e)
{
int j=1;
Linklist p;
p=L->hj;
while (p&&j<i) {
p=p->hj;
++j;
}
*e = p->date;
}
void shuchu(Linklist l)
{
while (l->hj != NULL) {
l = l->hj;
printf("%d\n",l->date);
}
}
void genggai(Linklist *L,int i,int e)
{
int j=1;
Linklist p;
p = (*L);
while (p&&j<i) {
p = p->hj;
j++;
}
p->hj->date = e;
printf("更改后的数据链为:\n");
while ((*L)->hj != NULL) {
(*L) = (*L)->hj;
printf("%d ",(*L)->date);}
printf("\n");
}
void tianjia(Linklist *L,int i,int e)
{
int j = 1;
Linklist p,q;
p = (*L);
while (p&&j<i) {
p = p->hj;
j++;
}
q = (Linklist)malloc(sizeof(Node));
q->date = e;
q->hj = p->hj;
p->hj->qu = q;
p->hj = q;
q->qu = p;
while ((*L)->hj != NULL)
{
(*L) = (*L)->hj;
printf("%d ",(*L)->date);
}
printf("\n");
}
void shanchu(Linklist *L,int i,int *e)
{
int j = 1;
Linklist p;
p = (*L);
while ((p->hj||p->qu)&&j<=i)
{
p=p->hj;
j++;
}
*e=p->date;
if (p->hj==NULL) {
p->qu->hj=NULL;
free(p);
}
else {p->qu->hj=p->hj;
p->hj->qu=p->qu;
free(p);}
}
int main(int argc, const char * argv[])
{
Linklist l,s,h;
printf("输入你要输入多少个数据\n");
int many;
scanf("%d",&many);
chuangjian(&l, many);
s=l;h=l;
shuchu(l);
int i=8;
printf("输入你要查找第多少个数据\n");
int t;
scanf("%d",&t);
chazhao(l,t, &i);
printf("查找数据为%d\n",i);
printf("输入修改位置,替换数据\n");
int place,sold;
scanf("%d,%d",&place,&sold);
genggai(&l,place,sold);
printf("输入添加第几个位置,添加数据\n");
int p,m;
scanf("%d,%d",&p,&m);
tianjia(&s, p, m);
printf("输入删除第几个位置\n");
int w;
scanf("%d",&w);
shanchu(&h,w,&i);
while (h->hj!=NULL) {
h=h->hj;
printf("%d ",h->date);
}
printf("\n");
printf("删除数据是%d\n",i);
return 0;
}
双链表的增删改查的功能实现代码。
最新推荐文章于 2023-03-28 21:34:14 发布
1. 双链表比单链表在结构上多了一个前屈指针。这个前驱指针会指向该节点的前一个结点。所以其中的任意一个结点都能找到他的前一个结点位置并进行访问,也能找到他的下一个结点位置并进行访问。