歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言之雙向鏈表

C語言之雙向鏈表

日期:2017/3/1 9:35:20   编辑:Linux編程

1,雙向鏈表簡介。

雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個數據結點中都有兩個指針,分別指向直接後繼和直接前驅。所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和後繼結點。一般我們都構造雙向循環鏈表。

2,例子要求:

完成雙向鏈表的插入、刪除以及查找,將學生管理系統使用的數組,以雙向鏈表的方式實現,能夠支持無限制的學生人數的增刪改查以及保存。

3,代碼實現。

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>


typedef struct Student{
char name[20];
int score;
char phoneNum[14];
} str_student;


typedef struct Node{
str_student data;
struct Node *prior; //指向前驅結點
struct Node *next; //指向後繼結點
}Node, *DLinkList;


// 初始化一個學生鏈表
DLinkList initDouLinkList()
{
Node *L,*p,*r;
char name[20];
char phone[14];
int score;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
r = L;
r->next = NULL;


while(1)
{
p = (Node *)malloc(sizeof(Node));
printf("input name is out exit,input student name:\n");
scanf("%s",name);
if (strcmp(name,"out")==0)
{
break;
}
strcpy(p->data.name, name);
printf("input student score:");
scanf("%d",&score);
p->data.score = score;
printf("input student phone:");
scanf("%s",phone);
strcpy(p->data.phoneNum, phone);


p->next = r->next;
r->next = p;
r = p;


}
r->next = NULL;
return L;
}


//添加學生信息
DLinkList insertDouLinkListStuent(DLinkList L,int i,char *name, int score,char *phonenum)
{
DLinkList p,s;
p = L->next;
int tempi;
for(tempi = 1;tempi < i-1; tempi++)
p = p->next;
s = (Node *)malloc(sizeof(Node));
s->data.score = score;
strcpy(s->data.name,name);
strcpy(s->data.phoneNum,phonenum);
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;


return L;
}


// 查找學生信息
int findDouLinkListStudent(DLinkList L,char *name)
{
DLinkList p;
p = L->next;
int i = 1;


while(p != NULL && (strcmp(p->data.name, name)!=0))
{
++i;
p = p->next;
}
if(p == NULL)
return 0;
else return i;
}


// 移除一個學生
DLinkList removeDouLinkListStudent(DLinkList L,char *name)
{
int tempi = 1;
DLinkList p;
p = L->next;
int i =findDouLinkListStudent(L,name);
while((tempi++) != i && p != NULL)
{
p = p->next;
}
if(p == NULL)
printf("no list \n");
else if(p->next == NULL)
{
p->prior->next = NULL;
free(p);
}
else
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
return L;
}


// 鋪助打印信息
void printfInfo(DLinkList L)
{
DLinkList p;
p = L->next;
while (p!=NULL)
{
printf("student name %s\n",p->data.name);
printf("student name %d\n",p->data.score);
printf("student name %s\n",p->data.phoneNum);
p=p->next;
}
}


void main ()
{
char name2[20]="hanmeimei";
char phone2[14]="13612345678";


DLinkList L =initDouLinkList();
// 2.1 初始化學生雙向鏈表數據
insertDouLinkListStuent(L,1,name2,99,phone2);
printfInfo(L);


// 2.2 查找學生zhangsan
findDouLinkListStudent(L,'zhangsan');
printfInfo(L);


// 2.3 刪除學生zhangsan
removeDouLinkListStudent(L,'zhangsan');
printfInfo(L);


// 2.4 添加學生zengteng
insertDouLinkListStuent(L,9,'zengteng',89,'13643345667');
printfInfo(L);


}

C++ 隱式類類型轉化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C語言變長數組之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C語言需要注意的問題 http://www.linuxidc.com/Linux/2013-05/84301.htm

C語言位域的使用及其注意點 http://www.linuxidc.com/Linux/2013-07/87027.htm

C語言中簡單的for循環和浮點型變量 http://www.linuxidc.com/Linux/2013-08/88514.htm

Copyright © Linux教程網 All Rights Reserved