数据结构实验一--单链表的基本操作的算法
一、实验环境
VC++ 6.0
Windows XP/7
二、程序基本设计 (一)、存储结构的类型定义typedef struct LNode/*定义单链表结点类型*/{ElemType data; struct LNode *next;} LinkList;(二)、单链表示意图

(三)、项目组成图

(四)、在项目中建立algo2_2.cpp的程序文件
其中包含的函数原型及功能是:
详情见代码,略去,,几百字。。
(五)、在实验一的项目中,建立名为exp2_2.cpp程序文件
其中包括的内容为:
详情见代码,略去几十字。。。。
(六)、实验一的项目的模块结构,及函数调用关系图
三、程序代码
代码如下:
/*文件名:exp2-2.cpp*/#include <stdio.h>#include <malloc.h>typedef char ElemType;typedef struct LNode/*定义单链表结点类型*/{ElemType data; struct LNode *next;} LinkList;extern void InitList(LinkList *&L);extern void DestroyList(LinkList *&L);extern int ListEmpty(LinkList *L);extern int ListLength(LinkList *L);extern void DispList(LinkList *L);extern int GetElem(LinkList *L,int i,ElemType &e);extern int LocateElem(LinkList *L,ElemType e);extern int ListInsert(LinkList *&L,int i,ElemType e);extern int ListDelete(LinkList *&L,int i,ElemType &e);void main(){int pos;LinkList *h;ElemType e;char ele;printf("(1)初始化单链表h\n");InitList(h);printf("\n(2)依次采用尾插法插入a,b,c元素\n");ListInsert(h, 1, 'a');ListInsert(h, 2, 'b');ListInsert(h, 3, 'c');printf("\n(3)输出单链表h:");DispList(h);printf("\n(4)单链表h长度=%d\n",ListLength(h));printf("\n(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));Sur:printf("\n(6)请输入你要查询的数据元素的位置(数字):");scanf("%d", &pos) ;if(GetElem(h,pos,e)){printf("单链表h的第%d个元素=%c\n", pos, e);}else{printf("查询失败!\n");SurEle:printf("你是否要继续查询?(Y/N)") ;getchar();scanf("%c", &ele) ;switch (ele){case 'Y':case 'y': goto Sur ; break;case 'N':case 'n': printf("您选择放弃再次查询!\n") ; break; default: printf("您输入的数据不合法!请重新输入:\n"); goto SurEle ; break;}}SurCon:printf("\n(7)请输入你要查找的数据元素的值:") ;getchar();scanf("%c", &e) ;if (LocateElem(h,e)){printf("查找成功,元素%c的位置=%d\n", e, LocateElem(h,e));} else{printf("查询失败!你输入的数据元素的值不存在!\n") ;SurConEle:printf("你是否要继续查询?(Y/N)") ;getchar();scanf("%c", &ele) ;switch (ele){case 'Y':case 'y': goto SurCon ; break;case 'N':case 'n': printf("您选择放弃再次查询!\n") ; break; default: printf("您输入的数据不合法!请重新输入:\n"); goto SurConEle ; break;}}printf("\n(8)请输入你要插入的数据元素的位置和数据元素值(以逗号隔开):");scanf("%d,%c", &pos, &e);if (ListInsert(h, pos, e)){printf("输出成功插入元素后的单链表h:");DispList(h);}Dle:printf("\n(10)请输入你要删除的数据元素的位置:");//getchar();scanf("%d", &pos) ;if(ListDelete(h,pos,e)){printf("删除成功!你删除的位置的数据元素值为%c\n", e) ;printf("输出成功删除元素后单链表h:");DispList(h);printf("你是否要继续删除?(Y/N)") ;getchar();scanf("%c", &ele) ;switch (ele){case 'Y':case 'y': goto Dle ; break;case 'N':case 'n': printf("您选择不再继续删除!\n") ; break; default: printf("您输入的数据不合法!请重新输入:\n"); goto Dle ; break;}}else{printf("删除失败!请检查你输入的数据元素的位置!\n") ;DleEle:printf("你是否要继续再次尝试删除?(Y/N)") ;getchar();scanf("%c", &ele) ;switch (ele){case 'Y':case 'y': goto Dle ; break;case 'N':case 'n': printf("您选择放弃尝试删除!\n") ; break; default: printf("您输入的数据不合法!请重新输入\n"); goto DleEle ; break;}}printf("\n(12)释放单链表h\n");DestroyList(h);}- 1楼hu1020935219昨天 21:30
- 双向链表。也要写一下。