链表对于C的基础掌握起着很大的作用,写一个双向链表对老手可能是小菜一碟,但对于初学者来说就很有难度了。
下面我说明一下数组和链表的区别:
1. 数组必须事先定义固定的长度(元素个数),不能适应数据动态地增减的情况。当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费;数组可以根据下标直接存取。
2. 链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项。(数组中插入、删除数据项时,需要移动其它数据项,非常繁琐)链表必须根据next指针找到下一个元素
3. (静态)数组从栈中分配空间, 对于程序员方便快速,但是自由度小
4. 链表从堆中分配空间, 自由度大但是申请管理比较麻烦
我一学生信息威力实现代码如下:
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
typedef struct student
{
int student_no;
float student_score;
struct student *next ,*pre;
}student,*student_p;
student_p student_create(student stud);
void student_print(student_p stu);
student_p student_search_by_no (student_p stu ,int student_num);
student_p student_insert(student_p stu ,student studen ,int lnum);
student_p student_delete_by_no (student_p stu ,int student_num);
int main(int argc, char* argv[])
{
int n = 0;
int lnum = 0;
int operate = 0;
int student_num = 0;
student stud = {0, 0.0, NULL};
student_p stu = NULL;
for(n=0; n<40; n++)
{
printf("INPUT OPERATE /n1:CREATE 2:SEARCH 3:INSERT 4:DELETE/n");
scanf("%d",&operate);
switch (operate)
{
case 1: printf("INPUT NO,SCORE LIKE(1,22。。。)AND(-1,-1)OVER:");
scanf("%d,%f",&stud.student_no,&stud.student_score);
stu = student_create(stud);
student_print(stu);
break;
case 2: printf("CHOOSE SEARCH NO.:");
scanf("%d",&student_num);
stu = student_search_by_no (stu,student_num);
student_print(stu);
break;
case 3: printf("INPUT NO.SCORE AND LOCATION:");
scanf("%d,%f",&stud.student_no,&stud.student_score);
scanf("%d",&lnum);
stu = student_insert(stu ,stud ,lnum);
student_print(stu);
break;
// case 'd':
case 4: printf("INPUT DELETENO.:");
scanf("%d",&student_num);
stu = student_delete_by_no (stu,student_num);
student_print(stu);
break;
default:
break;
}
}
}
//==============================student_create====================
student_p student_create (student stud)
{
int n = 0;
student_p stu = NULL;
student_p p1 = NULL;
student_p p2 = NULL;
p1 = p2 = (student_p)malloc(sizeof(student));
while (stud.student_no != -1)
{
n = n + 1;
if ( n == 1)
{
stu = p1;
}
else
{
p2->next = p1;
p2 = p1;
p1 = (student_p)malloc(sizeof(student));
scanf("%d,%f",&stud.student_no,&stud.student_score);
}
p1->student_no = stud.student_no;
p1->student_score = stud.student_score;
}
// p2->next = NULL;
return stu;
}
//==============================student_search====================
student_p student_search_by_no (student_p stu,int student_num )
{
student_p p1 = NULL;
student_p p2 = NULL;
p1 = stu;
while(student_num != p1->student_no && p1->next!=NULL)
{
p2 = p1 ;
p1 = p1->next;
}
if (student_num == p1->student_no)
{
printf("student_NO:%d/tstudent_SCORE:%f。",p1->student_no,p1->student_score);
}
return stu;
}
//==================================student_insert=======================
student_p student_insert(student_p stu ,student studen ,int lnum)
{
int j = 0;
student_p p1 = NULL;
student_p p2 = NULL;
p1 = stu;
student_p newp = (student_p)malloc(sizeof(student));
newp->student_no = studen.student_no;
newp->student_score = studen.student_score;
if (lnum == 0)
{
newp->next = p1;
p1->pre = newp;
stu = newp;
}
else
{
while (p1 && j < lnum-1)
{
p2 = p1;
p1 = p1->next;
++j;
}
if(p1->next == NULL)
{
newp->pre = p1;
p1->next = newp;
}
else
{
newp->pre = p1;
newp->next = p1->next;
p1->next->pre = newp;
p1->next = newp;
}
}
return stu;
}
//==============================student_delete====================
student_p student_delete_by_no (student_p stu ,int student_num)
{
student_p p1 = NULL;
student_p p2 = NULL;
p1 = stu;
while(student_num != p1->student_no && p1->next!=NULL)
{
p2 = p1 ;
p1 = p1->next;
}
if (student_num == p1->student_no)
{
if (student_num == stu->student_no)
{
stu = p1->next;
p1->next->pre = stu;
}
else
{
if(p1->next == NULL)
{
p2->next = NULL;
p1->pre = NULL;
}
else
{
p1->next->pre = p2;
p2->next = p1->next;
}
}
}
free(p1);
return stu;
}
//==============================student_print===================
void student_print(student_p stu)
{
student_p tp = NULL;
// printf("%p/n", head);
tp = stu;
printf("DATA LIST:/n");
if(tp == NULL)
{
return;
}
while(tp != NULL)
{
printf("student_NO:%d/tstudent_SCORE:%f/n", tp->student_no,tp->student_score);
tp = tp->next;
}
printf("/n");
}