《数据结构》基本操作指导

  • 格式:doc
  • 大小:266.50 KB
  • 文档页数:55

目录指导一、单链表的操作----------------------------------------------------- 2指导二、栈及其应用------------------------------------------------------- 10指导三、串的基本操作---------------------------------------------------- 16指导四、二叉树的基本操作---------------------------------------------- 21指导五、图的存储和遍历------------------------------------------------- 31指导六、查找--------------------------------------------------------------- 41 指导七、排序--------------------------------------------------------------- 49指导一、单链表的操作一、指导目的1、掌握线性表的链式存储结构。

2、掌握利用链式存储结构实现线性表的基本操作。

3、掌握链式存储结构中的算法实现。

二、指导内容1、建立带头结点的单链表,并输出该单链表。

2、实现带头结点的单链表上的插入、删除、查找、修改操作。

三、操作指导1、定义单链表的结点结构单链表的结点结构可为一个结构体类型(slnodetype),其成员是数据域和指针域,数据域可以是整数。

2、模块划分和程序控制流程根据实验要完成的各功能,设置初始化、建立单链表、输出单链表、插入、删除、查找、修改和主函数8个模块,对于要完成的各功能,采用适当的人机界面,用循环和分支结构构成菜单进行选择。

3、初始化模块int initiate(slnodetype **h)该模块中产生一个只有头结点的空单链表,用指针h作为函数的参数返回,因为h是指针变参,所以在函数的参数位置要以二级指针出现。

在函数里,申请一个头结点空间。

4、建立单链表模块int createlink(slnodetype *h)该模块中建立有若干个结点的单链表,用循环控制输入若干个整数,申请相应的结点空间,以输入的整数作为结点中的数据,依次链接到初始只有头结点的单链表h中,可以把输入0作为建立链表的结束。

5、输出单链表模块void display(slnodetype *h)对于传入的单链表h,依次输出单链表中的结点(数据)。

6、插入结点模块int inserti(slnodetype *h)设在第i个结点前插入数据为data的结点。

在该函数模块中输入i和数据data,对于传入的单链表h,先查找是否存在插入的位置(单链表h中至少要有i-1个结点),若不存在插入位置,则不做任何操作;若存在插入位置,则申请一个结点,其数据为data,挂在第i-1个结点的后面。

7、删除结点模块int delete(slnodetype *h)在该函数模块中,首先可以调用输出模块输出传入的单链表h,以便选择要删除的结点,然后输入要删除结点的数据data,再查找是否存在要删除的结点,若不存在要删除的结点,则显示相应的信息;若存在要删除的结点,则删除该结点(包括删除该结点空间)。

8、查找模块int search(slnodetype *h)在该函数模块中,首先可以调用输出模块输出传入的单链表h,以便选择要查找的结点,然后输入要查找结点的数据data,再查找该结点是否存在,若不存在要查找的结点,则显示相应的信息;若存在要查找的结点,也显示相应的信息。

9、修改模块int modify(slnodetype *h)在该函数模块中,首先可以调用输出模块输出传入的单链表h,以便选择要修改的结点,然后输入要修改结点的数据data,再查找该结点是否存在,若不存在要查找的结点,则显示相应的信息;若存在要查找的结点,则显示原结点的数据,再提示输入新的数据,输入新的数据后,可以再调用输出模块输出修改结点数据后的单链表h,以便查看修改后的单链表h中的数据。

10、主函数main()主函数中定义指向单链表的指针等变量,首先调用初始化操作initiate( ),考虑人机界面,进入如下操作菜单的循环控制结构:========== menu ==========1--- create 2--- display3--- insert 4--- delete5--- search 6--- modify0--- exitchoose the number between 0 to 6:对于要进行的操作,进入接受选择的循环控制(对于不合法的选择,重新提示选择),对与合法的选择,退出接受选择的循环控制,进入多分支结构,以执行相应的功能,执行完毕后回到操作菜单的循环控制中,依然显示菜单,提示选择。

当输入0(选择exit),则程序实行结束。

四、算法实现#include "stdio.h"#include "alloc.h"#include "stdlib.h"typedef struct slnode{int data;struct slnode *next;}slnodetype;int initiate(slnodetype **h){if((*h=(slnodetype *)malloc(sizeof(slnodetype)))==NULL) return 0;(*h)->next=NULL;return 1;}int createlink(slnodetype *h){int i=1,data;slnodetype *p,*s;printf("\n create link \n");p=h;printf(" NO: %d: ",i); scanf("%d",&data);while(data!=0){s=(slnodetype *)malloc(sizeof(slnodetype)); s->data=data;s->next=NULL;p->next=s;p=s;printf(" NO: %d: ",++i); scanf("%d",&data); }}void display(slnodetype *h){slnodetype *p;printf("\n linklist:\n");p=h->next;while(p){printf("%5d",p->data);p=p->next;}}int inserti(slnodetype *h){slnodetype *s,*p;int i,data,j=0;printf("\n input i: ");scanf("%d",&i);printf(" input data: ");scanf("%d",&data); p=h;while(p!=NULL&&j<i-1){p=p->next;j++;}if(p!=NULL){s=(slnodetype *)malloc(sizeof(slnodetype)); s->data=data;s->next=p->next;p->next=s;}}int delete(slnodetype *h){slnodetype *p,*q;int data;display(h);printf("\n input data: ");scanf("%d",&data);q=h;p=h->next;while(p!=NULL&&p->data!=data){q=p;p=p->next;}if(p==NULL) printf("\n data %d nod found!",data); else{q->next=p->next;free(p);}}int search(slnodetype *h){slnodetype *p,*q;int data;display(h);printf("\n input data: ");scanf("%d",&data);q=h;p=h->next;while(p!=NULL&&p->data!=data){q=p;p=p->next;}if(p==NULL) printf("\n data %d nod found!",data); else printf("\n data find!");}int modify(slnodetype *h){slnodetype *p,*q;int data;display(h);printf("\n input data: ");scanf("%d",&data);q=h;p=h->next;while(p!=NULL&&p->data!=data){q=p;p=p->next;}if(p==NULL) printf("\n data %d nod found!",data); else{printf("\n old data: %d",p->data);printf("\n new data: "); scanf("%d",&(p->data));display(h);}}main(){slnodetype *la;char ch;initiate(&la);clrscr(); ch=10;while(ch!=48){clrscr();textcolor(13);gotoxy(28,3);cprintf("==========");textcolor(14);gotoxy(38,3);cprintf(" menu ");textcolor(13);gotoxy(44,3);cprintf("==========");textcolor(12);gotoxy(24,5);cprintf("1---");textcolor(10);gotoxy(29,5);cprintf("create");textcolor(12);gotoxy(46,5);cprintf("2---");textcolor(10);gotoxy(51,5);cprintf("display");textcolor(12);gotoxy(24,7);cprintf("3---");textcolor(10);gotoxy(29,7);cprintf("insert");textcolor(12);gotoxy(46,7);cprintf("4---");textcolor(10);gotoxy(51,7);cprintf("delete");textcolor(12);gotoxy(24,9);cprintf("5---");textcolor(10);gotoxy(29,9);cprintf("search");textcolor(12);gotoxy(46,9);cprintf("6---");textcolor(10);gotoxy(51,9);cprintf("modify");textcolor(12);gotoxy(36,11);cprintf("0---");textcolor(10);gotoxy(41,11);cprintf("exit");ch=10;while(ch<48 || ch>64){textcolor(11);gotoxy(16,13);cprintf("choose the number between 0 to 6: "); textcolor(14);ch=getche();}switch(ch){case '1': createlink(la);break;case '2': display(la);break;case '3': inserti(la);break;case '4': delete(la);break;case '5': search(la);break;case '6': modify(la);break;}getch();}}五、运行和测试结果========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 1create linkNO: 1: 15NO: 2: 23NO: 3: 46NO: 4: 8NO: 5: 76NO: 6: 59NO: 7: 61NO: 8: 2NO: 9: 30NO: 10: 0========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 2linklist:15 23 46 8 76 59 61 2 30========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 3input i: 5input data: 28========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 2linklist:15 23 46 8 28 76 59 61 2 30========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 4linklist:15 23 46 8 28 76 59 61 2 30input data: 100data 100 not found!========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 4linklist:15 23 46 8 28 76 59 61 2 30input data: 76========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 2linklist:15 23 46 8 28 59 61 2 30========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify0--- exitchoose the number between 0 to 6: 5linklist:15 23 46 8 28 59 61 2 30input data: 59data find!========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 6linklist:15 23 46 8 28 59 61 2 30input data: 46old data: 46new data: 89linklist:15 23 89 8 28 59 61 2 30========== menu ========== 1--- create 2--- display 3--- insert 4--- delete 5--- search 6--- modify 0--- exitchoose the number between 0 to 6: 0指导二、栈及其应用一、指导目的1、掌握栈的数据类型描述及栈的特点。