http://lvguohaolcj.blog.163.com/blog/static/31646731201073121340250/
一、循环链表
循环链表是与单链表一样,是一种链式的存储结构,所不同的是,循环链表
的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而
构成一个环形的链;
循环链表和单链表的运算不同点:
1、在建立一个循环链表时,必须使其最好一个结点的指针指向表头结点,而
不是象单链表那样置为NULL,此中情况还使用于在最后一个结点后插入一个
新的结点;
2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值
等于表头指针时,说明已经表尾,而非象单链表那样判断链域值是否为NULL。
二、双向链表
当我们对单链表进行操作时,有时你要对某个结点的直接前驱进行操作时,
又必须从表表头开始查找。这是由单链表结点的结构所限制的,因为单链表每个
结点只有一个存储直接后继结点地址的链域,那么能不能定义一个既有存储直接
后继结点地址的链域,又有存储直接前驱结点地址的链域的这样一个双链域结点
结构呢?这就是双向链表。
在双向链表中,结点除含有数据域外,还有两个链域,一个存储直接后继
结点地址,一般称之为右链域;一个存储直接前驱结点地址,一般称之为左链域。
*/
//双向链表
typedef struct node
{
char name[20];
struct node *llink,*rlink;
}stud;
stud* creat(int n)
{
stud *p,*h,*s;
int size = sizeof(stud);
if ((h= (stud*) malloc(size)) == NULL)
{
printf("malloc failt");
exit(0);
}
//创建一个空的结构指针空间;
h->name[0] = '\0';
h->llink = NULL;
h->rlink =NULL;
p = h;
for (int i=0;i<n;i++)
{
if ((s = (stud*) malloc(size)) == NULL)
{
printf("malloc failt");
exit(0);
}
p->rlink = s;
printf("请输入第%d个人的姓名",n+1);
scanf("%s",s->name);
s->llink = p;
s->rlink = NULL;
s= p;
}
h->llink =s;
p->rlink = h;
return (h);
}
/*
假若我们要在一个表头的双向循环链表中查找数据域为一特定值的某个结点时,
我们同样从表头结点往后一次比较各结点数据域的值,若正是该特定值,则返
回指向结点的指针,否则继续往后查,直到表尾。
*/
stud* seach(stud *h,char* x)
{
stud *p;
char *y;
p = h->rlink;
while(p != h)
{
y = p->name;
if (strcmp(y,x) == 0)
{
return (p);
}
else
{
p= p->rlink;
}
}
printf("没有查找到该数据");
}
void print(stud *h)
{
stud *p;
p = h->rlink;
printf("数据信息为:\n");
while(p != h)
{
printf("%s ",&*(p->name));
p=p->rlink;
}
printf("\n");
}
/*
对于双向链表,我们可以随意在某个结点前或后插入一个新的结点;
假诺s,p,q是连续三个结点的指针,若我们要p前插入一个新结点r,
则只需把s的右链域指针指向r,r的左链域指针指向s,r的右链域指针指向
p,p的左链域指针指向r即可。
*/
void insert(stud* p)
{
char stuname[20];
stud *s;
if ((s= (stud*)malloc(sizeof(stud))) ==NULL)
{
printf("malloc Fail");
exit(0);
}
printf("请插入人的姓名:");
scanf("%s",stuname);
strcpy(s->name,stuname);
s->rlink = p->rlink;
p->rlink = s;
s->llink = p;
(s->rlink)->llink =s;
}
/*
删除某个结点,其实就是插入某个结点的逆操作,还是对于双向循环,要在连续三个结点
s,p,q中删除p点,只需把s的右链域指针指向q,q的左链域指针指向s,并回收p结点。
*/
void del(stud *p)
{
(p->rlink)->llink = p->llink;
(p->llink)->rlink = p->rlink;
free(p);
}
C语言实现双向链表
2009年03月23日 星期一 下午 03:34
/************双向链表*******/ typedef struct LIST int main (void) do return (List); /** 插入表 **/ This = Get_piout (List, i); temp = (LIST*) malloc (sizeof (LIST)); temp->element = element; return 0; /*** 删除元素 ***/ This = Get_piout (List, i); p = This->next; return 0; /**** 找位置函数 ***/ This = List; while ((This != NULL)&& (j < i - 1)) if ((This == NULL) || (j > i - 1)) /****打印表***/ q = List->next; if(q == NULL) while (q->next != NULL)
system ("cls"); fflush (stdin); |