单链表的创建

  • 格式:docx
  • 大小:231.61 KB
  • 文档页数:11

单链表的创建

指导教师: 王 亚 宁

实验时刻: 2011年 10 月 11 日

学院: 信息技术学院

专业: 运算机科学与技

班级 : 2020级2 班

姓名: 彭 伟

学号 : 202011010220

实验室 机 房

实验题目:对单链表的操作

实验目的:了解和把握线性表的逻辑结构和链式存储结构,把握单链表的大体算法及相关的时刻性能分析。

实验要求:成立一个数据域概念为字符串的单链表,在链表中不许诺有重复的字符串;依照输入的指令,显示链表信息,然后对链表进行操作。

实验要紧步骤:

1、分析、明白得程序。

2、写源代码

3、调试程序,并设计输入字符串数据,测试程序的功能。

4、修改程序:

程序清单:

#include

#include

#include

struct node /*概念节点的数据结构*/

{

int num;

char str[20];

struct node *next;

};

/*****************以下是主函数*************************/

main()

{

struct node *creat(); /*函数声明*/

struct node *insert(); /*函数声明*/

struct node *delet(); /*函数声明*/

void print(); /*函数声明*/

struct node *head;

char str[20];

int n;

head=NULL; /*做空表*/

head=creat(head); /*挪用函数创建以head为头的链表*/

print(head);

printf("please input number,name\n");

gets(str);

n=atoi(str);

gets(str);

head=insert(head,str,n); print(head);

printf("please input the delete name \n");

gets(str);

head=delet(head,str);

print(head);

return;

}

/********************以下是创建链表函数****************************/

struct node *creat(struct node *head)

{

char temp[30];

struct node *p1,*p2;

p1=p2=(struct node*)malloc(sizeof(struct node));

printf("please input number,name\n");

printf("leave, knock the enter twice\n");

gets(temp);

gets(p1->str);

p1->num=atoi(temp);

p1->next=NULL;

while(strlen(p1->str)>0);

{

if(head==NULL)

head=p1;

else

p2->next=p1;

p2=p1;

p1=(struct node *)malloc(sizeof(struct node));

printf("please input number, name\n");

printf("leave ,knock the enter twice\n");

gets(temp);

gets(p1->str);

p1->num=atoi(temp);

p1->next=NULL;

}

return head;

}

/********************以下是插入节点函数********************************/

struct node *insert(head,pstr,n)

struct node *head;

char *pstr;

int n;

{

struct node *p1,*p2,*p3;

p1=(struct node*)malloc(sizeof(struct node)); strcpy(p1->str,pstr);

p1->num=n;

p2=head;

if(head==NULL)

{

head=p1;

p1->next=NULL;

}

else

{

while(n>p2->num && p2->next!=NULL)

{

p3=p2;

p2=p2->next;

}

if(n<=p2->num)

if(head==p2)

{

head=p1;

p1->next=p2;

}

else

{

p3->next=p1;

p1->next=p2;

}

else

{

p2->next=p1;

p1->next=NULL;

}

}

return(head);

}

/***************************以下是删除节点函数************************/

struct node *delet(head,pstr)

struct node *head;

char *pstr;

{

struct node *temp,*p;

temp=head;

if(head==NULL)

printf("forms is Null\n");

else

{

temp=head;

while(strcmp(temp->str,pstr)!=0 && temp->next!=NULL)

{

p=temp;

temp=temp->next;

}

if(strcmp(temp->str,pstr)==0)

{

if(temp==head)

{

head=head->next;

free(temp);

}

else

{

p->next=temp->next;

printf("please input the delete part %s\n",temp->str);

free(temp);

}

}

else printf(" not find the delete part\n");

}

return(head);

}

/*************************以下是链表输出函数*******************************/

void print(struct node *head)

{

struct node *temp;

temp=head;

printf(" output forms\n");

while(temp!=NULL)

{

printf("\n%d- - - - &s\n",temp->num,temp->str);

temp=temp->next;

}

return;

}

运 行 结 果:

内容:创建包括学号、姓名节点的单链表。其节点数不限,表以学号为序,低学号的在前,高学号的在后,以输入姓名为空作终止。

在此链表中,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点。

思路:关于插入函数,利用molloc()函数向系统申请分派一个节点寄存输入的姓名和学号,

并让新的学号与各节点的学号比较,以确信插入的节点位于表头、表中或表尾。关于删除函数:

应依照制定的学生姓名,在链表中从头至尾依次查找各节点,并于各节点的学生姓名比较,

假设查找成功(相同)那么修改指针并释放内存空间,不然提示找不到节点后退出。

总结:在做那个链表时借助书本和网上的相关信息将其弄好,等程序运行成功不知有多少的困难,要不是非法字符确实是语法错误等一系列问题,可能是我对链表的不熟悉,还有确实是平常编程太少致使的,因此咱们以后应该动手动脑才行,不然四年事后咱们一无所得啊!天才是百分之一的灵感加百分之九十九的汗水所炼成的!