1、中北大学算法与数据结构实验报告课案 实验类别:算法与数据结构 专 业:信息与计算科学 班 级:13080241 学 号:1308024120 姓 名:杨燕 中北大学理学院实验一 链表的应用(一)建立线性表【实验内容】1、画出详细规范的算法流程图2、定义链表结点数据类型3、定义链表数据类型4、实现单向线性链表的建立5、实现单向线性链表取元素6、实现单向线性链表遍历7、实现单向线性链表插入8、实现单向线性链表删除【实验方法与步骤】1、定义链表结点数据类型typedef struct LNode int data; struct LNode *next;LNode,*LinkList;其中int d
2、ata;表示节点是整型数据,若定义浮点型的为:float data;其他类似。2、定义链表数据类型typedef char DateTypetypedef struct LNode DateType data; struct LNode *next;LNode,*LinkList;3、实现单向线性链表的建立#include #include #include typedef struct LNode int data; struct LNode *next;LNode,*LinkList;void CreateList_L(LinkList &L,int n) /逆位序输入n个数据元素的值,建
3、立带头结点的单链表L int i; LNode *p; L=(LinkList)malloc(sizeof(LNode); L-next=NULL;/先建立一个带头结点的空链表 cout请输入创建的单链表中的数据: 0;-i) p=(LinkList)malloc(sizeof(LNode);/生成新结点 cinp-data; p-next=L-next;/将新结点插入到单链表的头 L-next=p;/修改单链表头结点的指针域 /for结束 if(n) cout成功创建一个单链表!endl; else cout创建了一个空链表!endl; void main() LinkList L; int
4、 InitLNodeNum; coutCreateList_L.cppendl=endl; coutendlInitLNodeNum; CreateList_L(L,InitLNodeNum); coutOK.!endl; getch();/end of main() function4、实现单向线性链表取元素#include #include #include #define ElemType int #define LIST_MAX_LENGTH 100 /LIST_MAX_LENGTH是单链表L的最大长度typedef struct LNode ElemType data; struct
5、 LNode *next;LNode,*LinkList;void CreateList_L(LinkList &L,int n) /创建一个带头结点的单链表L int i; LNode *p; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; for(i=n;i0;-i) p=(LinkList)malloc(sizeof(LNode); cinp-data; p-next=L-next; L-next=p; int GetElem_L(LinkList L,int i,int &e) /GetElem_L() function/L为带头结点的单链
6、表的头指针,当第i个元素存在时,其值赋给e并返回OK, /否则返回Error LNode *p; int j=1; p=L-next; /初始化,p指向链表第一个结点,j为计数器 while(p&jnext;+j; if(!p|ji) cout这个元素 i 不存在!data; return (e); /结束单链表的取元素void main() /main() function LinkList L; int e; /e can be Every DataType int i,LListNodeNum; /j is a counter for cycle coutGetElem_L.cppend
7、l=endlendl; coutLListNodeNum; cout请输入创建的单链表中的数据: endl; CreateList_L(L,LListNodeNum); coutendl成功创建一个单链表L!endl; cout你想提取哪一个位置上的数据?: i; /输入要提取的数据 if(iLListNodeNum) coutendl输入错误!endl; GetElem_L(L,LListNodeNum-i+1,e); coutendl位置i 在单链表中的数据是: e; coutendl.OK.!endl; getch();5、实现单向线性链表遍历#include #include #inc
8、lude #include #define LIST_INIT_LENGTH 10 /LIST_INIT_LENGTH is the Init_Define_Length of LinkList typedef int ElemType; typedef struct LNode int data; struct LNode *next; LNode,*LinkList;void CreateList_L(LinkList &L,int n) /CreatList_L() subfunction /To Creatre a LinkList L with HeadNode int i; LNo
9、de *p; int arrayLIST_INIT_LENGTH; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; printf(Please input the nodes data: n); for(i=0;in;i+) /input the data to create the LinkList scanf(%d,&arrayi); for(i=0;idata=arrayi; /for example to a CreateList p-next=L-next; L-next=p; /end of for /end of CreateList
10、_L() function void Contray(LinkList &head) /Contray() function /Delete the NO.i element of LinkList and return by variable e LNode *p,*q; p=head; head=NULL; while(p) q=p; p=p-next; q-next=head; head=q; /end of while coutendlSuccess to Contray the LinkList !;/end of Contray() functionvoid main() /mai
11、n() function LinkList L; LNode *p; int i,LNodeNum; /j is just a counter for cycle coutContray.cppendl=endlendl; coutHow many nodes do you want to create? ; cinLNodeNum; CreateList_L(L,LNodeNum); p=L; coutendlThe next ones Inserted Direction is always in front of this.endl; for(i=0;inext; coutdata ;
12、/output the LinkList before Contray coutendl; cout-endl; Contray(L); /call function Contray(); coutendlthe LinkList after contray is: ; for(i=0;iLNodeNum;+i) coutdatanext; coutendl.OK.!endl; getch();/end of main() function6、实现单向线性链表插入# include # include # include # include # define INIT_LENGTH 10# d
13、efine OK 1# define ERROR 0typedef struct LNode /define LNode structure int data; struct LNode *next;LNode,*Linklist;int ListInsert_L(Linklist &L,int i,int e) /在带头结点的单链线性表L中第i个位置之前插入元素e LNode *p=L; int j=0; while(p&jnext; +j; if(!p|ji-1) /i小于1或i大于表长 cout错误!这个位置不存在!data=e; s-next=p-next; p-next=s; ret
14、urn (OK); /ListInsert_L() endvoid main() /main() function int i,j,e; LNode node10; LNode *L,*p; int arrayINIT_LENGTH+1=5,8,12,18,25,30,37,46,51,89; L=node; L=(Linklist)malloc(sizeof(LNode); L-next=NULL; for (i=10;i0;i-) p=(Linklist)malloc(sizeof(LNode); p-data=arrayi-1; p-next=L-next; L-next=p; p=L;
15、 coutendlendlListInsert_L.cpp; coutendl=; cout endlendl原来的单链线性表L为: ; for(i=0;inext; coutdata ; coutendlendlj; coute; if(ListInsert_L(L,j,e) cout endl新的单链线性表L为: ; p=L; for(i=0;inext; coutdata ; coutendlendl.OK!.; getch();7、实现单向线性链表删除#include #include #include typedef struct LNode int data; struct LNo
16、de *next;LNode,*LinkList;void CreateList_L(LinkList &L,int n) /CreateList_L() function /创建一个带头结点的单链表L int i; LNode *p; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; for(i=0;ip-data; p-next=L-next; L-next=p; /结束for/end of CreateList() functionint ListDelete_L(LinkList &L,int i,int &e) /ListDelete_L(
17、) function /在带头结点的单链线性表L中,删除第i个元素,并由e返回其值 LNode *p,*q; int j=0; p=L; while(p-next&jnext;+j; if(!p|ji-1) /删除位置不合理 cout位置i的数据不存在!next; /用指针q指向被删除的结点 p-next=q-next; /删除第i个结点 e=q-data; /取出第i个结点数据域值 free(q); /释放第i个结点 return (e); /结束删除元素void main() /main() function LinkList L; LNode *p; int e; /e can be E
18、very DataType int i,j; /j is just a counter for cycle int LListNodeNum; coutListDelete_L.cppendl=endlendl; coutLListNodeNum; coutendl请输入创建的单链表中的数据 endl; CreateList_L(L,LListNodeNum); coutendlnext; while(p) /输出创建的单链表 coutdatanext; /end of for coutendlendli; /输入要删除的位置 if(i= LListNodeNum) cout数据i已经被删除L
19、istDelete_L(L,i,e)endl; for(j=1;jnext; coutdata ; /输出新的单链表 /结束for coutendlendl.OK.!endl; /结束if else coutInput Error!endl; getch();/end of main() function【实验结果】1、写出实验的总结与体会,要简洁、真实、深刻;忌空话、套话2、单向链表和双向链表在实现时的区别3、单向链表如何修改实现循环链表实验二 链表的应用(二)栈的应用【实验内容】1、实现单向链栈的抽象数据类型2、实现单向链栈的建立、销毁、取栈顶元素、压栈、弹栈的运算3、给出包含括号和+、-
20、、*、四则运算的运算符优先级表4、创建运算符栈和运算数栈5、实现有一定通用性的程序,实现一个四则运算表达式的求解6、设计测试用的运算表达式,通过键盘输入进行测试【实验方法与步骤】1、构造空链式队列算法# include # include # include # define MAXQSIZE 100# define OK 1# define ERROR 0typedef int QElemType;typedef struct SqQueue/创建一个头结点 QElemType *base; int front; int rear;SqQueue;int InitQueue(SqQueue
21、&Q) Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType); if(!Q.base)/存储空间分配失败 coutendlOverflow ! ; return (ERROR); Q.front=Q.rear=0; return (OK); /InitQueue() endvoid main() /main function SqQueue Q; coutendlendlInitQueue.cpp; coutendl=; if(InitQueue(Q) coutendlendl成功!链式队列已被构造!; coutendlendl.OK!.;
22、 getch(); /main() end2、销毁链式队列算法# include # include # include # define MAXQSIZE 100# define OK 1# define ERROR 0typedef int QElemType;typedef struct QNode /define structure QNode QElemType data; struct QNode *next;QNode,*QueuePtr;typedef struct LinkQueue /define structure LinkQueue QueuePtr front; Qu
23、euePtr rear;LinkQueue;int EnQueue(LinkQueue &Q,QElemType e) /构造队列 QNode *p; p=(QueuePtr)malloc(sizeof(QNode); if(!p) coutendldata=e; p-next=NULL; if(Q.front=NULL) /new QNode Q.front=Q.rear=p; return (OK); Q.rear-next=p; Q.rear=p; return (OK); /EnQueue() endint DestroyQueue(LinkQueue &Q) /销毁队列Q while(Q.front) Q.rear=Q.front-next; free(Q.front); Q.front=Q.rear; return (OK); /DestroyQueue() endvoid main() /main() function int i,e=1; LinkQueue Q; QNode *q; Q.front=Q.rear=NULL; coutendlendlDestroyQueue.cpp; coutendl=endlendl; while(e) coute; if(e) EnQueue(Q,e)