1,双向链表简介。
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。
2,例子要求:
完成双向链表的插入、删除以及查找,将学生管理系统使用的数组,以双向链表的方式实现,能够支持无限制的学生人数的增删改查以及保存。
3,代码实现。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#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语言双向链表的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。