数据结构——双向链表,循环链表

来源:互联网 发布:通用网络加速 编辑:程序博客网 时间:2024/06/06 02:22

也许是自己太小看数据结构,练习了几天还在第二章徘徊,可自己觉得基础还是要打牢的好……

总结一下代码…………

第一个是双向链表,

#include<stdio.h>
#include<malloc.h>

typedef struct Node
{
int data;
struct Node *pre,*next;
}Node,*Linklist;

void creat(Linklist L)
{
Node *p,*r;

L->data=-1;
L->pre=L;
L->next=L;
r=L;
int c;
int flag=1;
printf("输入元素(以-1结束):/n");
while(flag)
{
   scanf("%d",&c);
   if(c!=(-1))
   {
    p=(Node*)malloc(sizeof(Node));
    p->data=c;
    r->next=p;
    p->pre=r;
    r=p;
   }
   else
   {
    flag=0;
    p->next=L;
    L->pre=p;
   }
}
}
int insert(Linklist L,int a, int b)
{
Node *p,*s;
p=L->next;
int j=1;
while(p!=L&&j<a-1)
{
   p=p->next;
   j++;
}
if(p==L)
{
   printf("插入位置不合理……");
   return 0;
}
s=(Node*)malloc(sizeof(Node));
if(s)
{
   s->data=b;
   s->next=p->next;
   p->next->pre=s;
   p->next=s;
   s->pre=p;
   return 1;
}
else
   return 0;

}
int del(Linklist L,int c)
{
Node *p;
p=L->next;
int j=1;
while(p!=L&&j<c-1)
{
   p=p->next;
   j++;
}
if(p==L)
{
   printf("/n删除位置不合法…………/n");
   return 0;
}
else
{
   p->next=p->next->next;
   p->next->next->pre=p;
}
return 1;
}

void main()
{
Linklist L;
int a,b;
int c;
Node *p;
L=(Node*)malloc(sizeof(Node));
printf("创建双向链表:/n");
creat(L);
p=L->next;
printf("/n显示链表:/n");
while(p!=L)
{
   printf("%d ",p->data);
   p=p->next;
}

printf("/n请输入要插入的位置和元素:/n");
scanf("%d,%d",&a,&b);
insert(L,a,b);

p=L->next;
printf("/n显示链表:/n");
while(p!=L)
{
   printf("%d ",p->data);
   p=p->next;
}

printf("/n请输入要删除的元素的位置:/n");
scanf("%d",&c);
del(L,c);

p=L->next;
printf("/n显示链表:/n");
while(p!=L)
{
   printf("%d ",p->data);
   p=p->next;
}

}
接下来是循环链表

#include<stdio.h>
#include<malloc.h>

typedef struct Note
{
int data;
struct Note *next;
}Note,*Linklist;

void create1(Linklist La)
{
Note *p;
int a;
La->data=-1;
La->next=La;
printf("请输入链表的元素(以-1结束):/n");
scanf("%d",&a);
while(a!=(-1))
{
   p=(Note*)malloc(sizeof(Note));
   p->data=a;
   p->next=La->next;
   La->next=p;
   scanf("%d",&a);
}

}
Linklist create2()
{
Linklist La;
La=(Note*)malloc(sizeof(Note));
Note *p;
int a;
La->data=-1;
La->next=La;
printf("请输入链表的元素:/n");
scanf("%d",&a);
while(a!=(-1))
{
   p=(Note*)malloc(sizeof(Note));
   p->data=a;
   p->next=La->next;
   La->next=p;
   scanf("%d",&a);
}
p=La;
while(p->next!=La)
{
   p=p->next;
}
return p;
}


Linklist merge1(Linklist la,Linklist lb)
{
Note *a,*b;
a=la->next;
b=lb->next;
while(a->next!=la) a=a->next;
while(b->next!=lb) b=b->next;
a->next=lb->next;
b->next=la;
free(lb);
return la;
}

Linklist merge2(Linklist La,Linklist Lb)
{
Note *p;
p=La->next;
La->next=Lb->next->next;
Lb->next=p;

return p;

}

void x1()
{
Linklist La,Lb,Lc;
Note *p;


//建立链表的方法一
printf("创建循环链表La:/n");
La=(Note*)malloc(sizeof(Note));
create1(La);
printf("/n显示链表La:/n");
p=La->next;
while(p!=La)
{
   printf("%d ",p->data);
   p=p->next;
}

printf("/n创建循环链表Lb:/n");
Lb=(Note*)malloc(sizeof(Note));
create1(Lb);
printf("/n显示链表Lb:/n");
p=Lb->next;
while(p!=Lb)
{
   printf("%d ",p->data);
   p=p->next;
}

printf("/n合并链表:/n");
Lc=merge1(La,Lb);
printf("/n显示合并链表Lb:/n");
p=Lc->next;
while(p!=Lc)
{
   printf("%d ",p->data);
   p=p->next;
}


}
void x2()
{
   Linklist La,Lb,Lc;
Note *p,*q;
//建立链表的方法二
printf("/n创建循环链表La:/n");
La=(Note*)malloc(sizeof(Note));
La=create2();
q=La->next->next;

printf("/n显示链表La:/n");
while(q!=La->next)
{
   printf("%d ",q->data);
   q=q->next;
}

printf("/n创建循环链表Lb:/n");
Lb=(Note*)malloc(sizeof(Note));
Lb=create2();
q=Lb->next->next;

printf("/n显示链表Lb:/n");
while(q!=Lb->next)
{
   printf("%d ",q->data);
   q=q->next;
}


printf("/n合并链表为:/n");
Lc=merge2(La,Lb);

printf("/n显示合并链表Lc:/n");
p=Lc->next;
while(p!=Lc)
{
   printf("%d ",p->data);
   p=p->next;
}

}
void main()
{
int mm;
printf("请输入你的操作:/n");
printf("1,法一 的链表合并/n");
printf("2,法二 的链表合并/n");
scanf("%d",&mm);
switch(mm)
{
case 1:
   x1();
   break;
case 2:
   x1();
   break;
default:
   printf("输入不合法……");
}
}

由于自己想学好编程,所以下的功夫也算…………都是自己编写的代码,虽然实现思想上基本一致,可自己怎么说是自己写的。亲自调试的,并且还做了教程——(这只是自己学习的方法——借鉴一位牛人的方法)

原创粉丝点击