链表的建立-插入-删除-输出全_第1页
链表的建立-插入-删除-输出全_第2页
链表的建立-插入-删除-输出全_第3页
免费预览已结束,剩余15页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、链表的建立-插入-删除-输全OOOL建立新的降P-OSS蠹中的壶ra1A§piLI n M.一建立新的髓実B盘I b=3inputnameofthe 1student:yuinputscareaFthe 1student:9BinputnaiHieofthe 2Liiput&icu>reuftlic 2s tudLcn : 33inputruaneofthe 3student;:uuinputscan reafthe 3student:871 is-t: E! lerTtRnt-sis :±前表中的元素input nante of the 1 &t7u

2、identr = ju InipuuL EcsTh uf tfnc 1 sLucicrnib»7B List elements is -时间:2011 年9月28日文件功能:实现了动态建立一个学生信息的链表包括链表的创建、插入、删除、和打印输出学生信息包括姓名和分数本链表是带有头结点的,头结点的内容为空内容*/* 包含头文件*/#in clude<stdio.h> #in clude<stdlib.h>#i nclude<malloc.h> #i ncludevstri ng.h>/* 结构体定义部分*/struct Nodechar n a

3、me10;int score;struct Node *n ext;;typedef struct Node ListNode;/* 函数声明部分*/* 函数实现部分*/* 创建链表*/*在链表的末端插入新的节点,建立链表*/ListNode *CreateList(int n)ListNode *head;指向头结点指针ListNode *p,*pre;int i;head=(ListNode *)malloc(sizeof(ListNode);为头节点分配内存空间head-> next二NULL;/将头结点的指针域清空pre二head;/先将头结点首地址赋给中间变量prefor(i=

4、1;i<=n;i+)通过for循环不断加入新的结点prin tf("i nput n ame of the %d stude nt:",i);打印出第几个人的名字p=(ListNode *)malloc(sizeof(ListNode);/为要插入的节点分配/内存空间p指向新插入结点的首地址sca nf("%s",&p-> name);/输入姓名prin tf("i nput score of the %d stude nt:",i);sca nf("%d",&p->score);

5、输入分数pre->next二p;将p指向新结点插入链表也就是头结点指针域指向/下个结点/第一个结点就是p指向的,因为头结点内容为空pre=p;/这个起着指向下一个结点的作用/* 输出链表*/void PrintList(ListNode *h)ListNode *p;p=h->n ext;while(p)prin tf("%s,%d",p->n ame,p->score);p=p- >n ext;prin tf("n");/* 插入链表结点*/*函数名称:InsertList(ListNode *h,int i,char n

6、ame,inte,int n)函数功能:插入链表结点入口参数:h:头结点地址i:插入到第几个结点name:插入结点的姓名e:插入结点的分数n:链表中结点的个数除下头结点外的个数出口参数:*/void In sertList(ListNode *h,i nt i,char n ame,i nt e,i ntn)ListNode *q,*p;先定义2个指向一个结点的指针int j;if(i<1 | i>n+1)prin tf("Error! Please in put aga in.n ”);elsej=0;p=h;将指针p指向要链表的头结点while(j<i-1)p=

7、p->n ext;j+;q=(ListNode *)malloc(sizeof(ListNode);/*为要插入的结点分配内存空间*/-赋值操作strcpy(q-> name, name); /将名字拷到要插入的节点内q->score=e; /将要插入的节点中分数赋值/调整指针域q-> next = p-> next; /*这个是将新插入的结点指针域指向上一个结点指针域指向的结点地址即为p-> next*/p-> next二q;/*将要插入结点位置前面的结点指针域指向现在插入的结点首地址*/*函数名称:DeleteList(ListNode *h, i

8、nt i, int n)函数功能:删除链表结点入口参数:h:头结点地址i:要删除的结点所在位置n:链表中结点的个数除下头结点外的个数出口参数:*/void DeleteList(ListNode *h, i nt i, i nt n)ListNode *p,*q;首先定义2个指向结点型结构体的指针int j;char n ame10;int score;if(i<1 | i>n)/如果位置超出了 1和n的范围的话则打印出错误信息prin tf("Error! Please in put aga in.n ”);else/没有超出除头结点外的1到n的范围的话那么执行删除操作

9、j=0;p=h;/将指针指向链表的头结点首地址while(j<i-1)p=p->n ext;j+;q=p-> next; /*q指向要删除的位置之前的那个结点指针域指向的地址q指向的结点就是要删除的结点*/p-> next二q-> next;/*这个就是将要删除的结点的前面那个结点的指针域指向要删除的结点指针域中存放的下个结点的首地址从而实现了删除第i个结点的作用*/strcpy( name,q-> name);score二q->score;free(q);/ 释放q指向的结点printf("n ame=%s,score=%dn",

10、 name,score);/* 主函数*/void mai n()ListNode *h;/h指向结构体 NODEint i = 1, n, score;char n ame 10;while ( i )/*输入提示信息*/prin tf("1-建立新的链表n");prin tf("2-添加元素n");prin tf("3-删除兀素n");prin tf("4-输出当前表中的兀素n");prin tf("0-退出n");sca nf("%d",&i);switch(i

11、)case 1:printf("n=");/*输入创建链表结点的个数*/sca nf("%d",&n);h=CreateList(n);/*创建链表 */printf("list elements is : n");Prin tList(h);break;case 2:printf("input the position. of insert element:");sea nf("%d",&i);prin tf("i nput n ame of the stude nt

12、:");sea nf("%s", name);prin tf("i nput score of the stude nt:");sea nf("%d", &seore);In sertList(h,i ,n ame,seore ,n);printf("list elements is:n");Prin tList(h);break;ease 3:printf("input the position of delete element:");sea nf("%d",&i);DeleteList(h,i, name,score, n);print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论