当前位置:文档之家› 数据结构实验报告讲解

数据结构实验报告讲解

浙江师范大学实验报告学院:数理与信息工程学院专业:计算机科学与技术姓名:杨富生学号: 201531910137课程名称:数据结构指导教师:钟发荣实验时间: 2016-06-152016年6月15日实验一1.实验要求1.1掌握数据结构中线性表的基本概念。

1.2熟练掌握线性表的基本操作:创建、插入、删除、查找、输出、求长度及合并并运算在顺序存储结构上的实验。

2.实验内容2.1编写一个函数,从一个给定的顺序表A中删除元素值在x到y之间的所有元素,要求以较高效率来实现。

#include<stdio.h>typedef int elemtype;#define maxsize 10int del(int A[],int n,elemtype x,elemtype y){int i=0,k=0;while(i<n){if(A[i]>=x&&A[i]<=y)k++;elseA[i-k]=A[i];i++;}return(n-k);}void main(){int i,j;int a[maxsize];printf("输入%d个数:\n",maxsize);for(i=0;i<maxsize;i++)scanf("%d,",&a[i]);j=del(a,maxsize,1,3);printf("输出删除后剩下的数:\n");for(i=0;i<j;i++)printf("%d "\n,a[i]);}2.2试写一个算法,在无头结点的动态单链表上实现线性表插入操作INSERT(L,i,b)。

void Insert(Linklist &L,int i,elemtype x){if(!L){L=(Linklist)malloc(sizeof(Lnode));(*L).data=x;(*L).next=NULL;}else{if(i==1){s=(Linklist)malloc(sizeof(Lnode));s->data=x;s->next=L;L=s;}else{p=L;j=1;while(p&&j<i-1){j++;p=p->next;}if(p||j>i-1)return error;s=(Linklist)malloc(sizeof(Lnode));s->data=x;s->next=p->next;p->next=s;}}}2.3生成两个多项式PA和PB,求他们的和,输出“和多项式”。

typedef struct node{int exp;float coef;struct node *next;}polynode;polynode *polyadd(polynode *pa,polynode *pb){polynode *p,*q,*pre,*r;float x;p=pa->next;q=pb->next;pre=pa;while((p!=NULL)&&(q!=NULL))if(p->exp>q->exp){r=q->next;q->next=p;pre->next=q;pre=q;q=r;}elseif(p->exp==q->exp){x=p->coef+q->coef;if(x!=0){p->coef=x;s=p;}else{pre->next=p->next;free(p);}p=pre->next;r=p;q=q->next;free(r);}elseif(p->exp<q->exp){pre=p;p=p->next;}if(q!=NULL)pre->next=q;free(pb);}2.4设计一个统计选票的算法,输出每个候选人的得票结果。

typedef int elemtypetypedef struct linknode{elemtype data;struct linknode *next;}nodetype;nodetype *create(){elemtype d;nodetype h=NULL,*s,*t;int i=1;printf("建立单链表:\n");while(1){printf("输入第%d个结点数据域",i);scanf("%d",&d);if(d==0)break;if(i==1){h=(nodetype *)malloc(sizeof(nodetype));h->data=d;h->next=NULL;t=h;}else{s=(nodetype *)malloc(sizeof(nodetype));s->data=d;s->next=NULL;t->next=s;t=s;}i++;}return h;}void sat(nodetype *h,int a[]){nodetype *p=h;while(p!=NULL){a[p->data]++;p=p->next;}}void main(){int a[N+1],i;for(i=0;i<N;i++)a[i]=0;nodetype *head;head=create();sat(head,a);printf("候选人:");for(i=1;i<=N;i++) printf("%3d",i);printf("\n得票数\n");for(i=1;i<=N;i++)printf("%3d",a[i]);printf("\n");}3.实验心得体会线性表是最简单的、最常用的一种数据结构,是实现其他数据结构的基础。

实验二1.实验要求1.1了解栈和队列的特性,以便灵活运用。

1.2熟练掌握栈和有关队列的各种操作和应用。

2.实验内容2.1 设一个算术表达式包括圆括号,方括号和花括号三种括号,编写一个算法判断其中的括号是否匹配。

#include<stdio.h>#include<malloc.h>#include<string.h>#define NULL 0typedef struct list{char str;struct list *next;}list;void push(char,list *);int pop(char.list *);void deal(char *str);main(void){char str[20];printf("\n请输入一个算式:\n");gets(str);deal(str);printf("正确!");getchar();return 0;}void deal(char *str){list *L;L=(list *)malloc(sizeof(list));if(!L){printf("错误!");exit(-2);}L->next=NULL;while(*str){if(*str=='('||*str=='['||*str=='{')push(*str,L);elseif(*str==')'||*str==']'||*str=='}')if(pop(*str,L)){puts("错误,请检查!");puts("按回车键退出");getchar();exit(-2);}str++;}if(L->next){puts("错误,请检查!");puts("按任意键退出");getchar();exit(-2);}}void push(char c,list *L){list *p;p=(list *)malloc(sizeof(list));if(!p){printf("错误!");exit(-2);}p->str=c;p->next=L->next;L->next=p;}#define check(s) if(L->next->str==s){p=l->next;L->next=p->next;free(p);return(0);} int pop(char c,list *L){list *p;if(L->next==NULL)return 1;switch(c){case')':check('(') break;case']':check('[') break;case'}':check('{') break;}return 1;实验三1.实验要求1.1掌握二叉树,二叉树排序数的概念和存储方法。

1.2掌握二叉树的遍历算法。

1.3熟练掌握编写实现树的各种运算的算法。

2.实验内容2.1编写程序,求二叉树的结点数和叶子数。

#include<stdio.h>#include<malloc.h>struct node{char data;struct node *lchild,*rchild;}bnode;typedef struct node *blink;blink creat(){blink bt;char ch;ch=getchar();if(ch==' ') return(NULL);else{bt=(struct node *)malloc(sizeof(bnode));bt->data=ch;bt->lchild=creat();bt->rchild=creat();}return bt;}int n=0,n1=0;void preorder(blink bt){if (bt){n++;if(bt->lchild==NULL&&bt->rchild==NULL)n1++;preorder(bt->lchild);preorder(bt->rchild);}}void main(){blink root;root=creat();preorder(root);printf("此二叉数的接点数有:%d\n",n);printf("此二叉数的叶子数有:%d\n",n1);}2.2编写递归算法,求二叉树中以元素值为X的结点为根的子数的深度。

相关主题