理工大数据结构实验报告

实验项目 线性表

  • 操作方法与实验步骤

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 20

#define OK 1

#define ERROR 0

typedef int ElemType;

typedef int Status;

typedef struct

{

    ElemType data[MAXSIZE];

    int Length;

}SqList;

 

 

Status InitList(SqList * L)   //初始化线性表

{

    L->Length=0;

    return OK;

}

//创建一个有序链表

SqList Create(SqList * L,ElemType Number)

{

    int StartNumber,Step;

    printf("请输入初始值和两数相差的值:\n");

    scanf("%d%d",&StartNumber,&Step);

    for(int i=0;i<Number;i++)

    {

        L->data[i]=StartNumber+(i)*Step;

    }

    L->Length=Number;

    return *L;

}

//遍历链表

void print(SqList *L)

{

    for(int i=0;i<L->Length-1;i++)

    {

        printf("%d ,",L->data[i]);

    }

    printf("%d",L->data[L->Length-1]);

    printf("\n");

}

//判断插入位置

Status Locate(SqList *L,ElemType e)

{

    if(e<L->data[0])

    {

        return 0;

    }

    if(e>L->data[L->Length-1])

    {

        return L->Length;

    }

    else

    {

        for(int i=0;i<L->Length;i++)

        {

 

            if(e>=L->data[i] && e<L->data[i+1])

            {

                return i+1;

            }

        }

    }

}

//插入

Status InorderList(SqList *L,ElemType Location,ElemType e)

{

    for(int i=L->Length-1;i>=Location;i--)

    {

        L->data[i+1]=L->data[i];

    }

    L->data[Location]=e;

    L->Length++;

    return OK;

}

int main()

{

    SqList L;

    InitList(&L);

    int Number;

    printf("输入顺序表的元素个数:\n");

    scanf("%d",&Number);

    Create(&L,Number);

    print(&L);

    printf("请输入插入的元素:\n");

    int InputNumber;

    scanf("%d",&InputNumber);

    int Location=Locate(&L,InputNumber);

    InorderList(&L, Location,InputNumber);

    print(&L);

    return 0;

}

 

  • 实验数据记录和处理

图(1)

 

实验项目 树

一、操作方法与实验步骤

实习题1:编写递归算法,计算二叉树中叶子结点的数目

#include <stdio.h>

#include<stdlib.h>

struct node

{

char info;

struct node *llink, *rlink;

};

typedef struct node NODE;

NODE *create()

{

char x;

NODE *p;

scanf("%c", &x);

printf("%c", x);

if(x!='.')

{

p=(NODE *)malloc(sizeof(NODE));

p->info=x;

p->llink=create();

p->rlink=create();

}

else

p=NULL;

return p;

}

int run(NODE *t)

{

static int count=0;

if(t)

{

run(t->llink);

run(t->rlink);

if(t->llink ==NULL && t->rlink == NULL) { count++; }

}

return count;

}

int main()

{

NODE *T;

int left_number;

printf("请输入一棵树:\n" );

T=create();

printf("\n");

if(!T)

printf("This is a empty binary tree.");

else

{

left_number=run(T);

printf("\n这棵树共有%d 个子叶. \n", left_number);

}

printf("\n");

}

实习题2:编写递归算法,在二叉树中求位于先序序列中第K个位置的结点

#include<stdio.h>

#include<malloc.h>

int num = 1, count = 1,bre=0; //num:要输入的k值 count:结点计数 bre:退出递归

char res; // 返回结点信息

 

struct node {

    char info;

    struct node *llink, *rlink;};

typedef struct node NODE;

 

NODE *creat() { //构造二叉树

    char x;

    NODE *p;

    scanf("%c", &x);

    if (x != '.') {

        p = (NODE *) malloc(sizeof(NODE));

        p->info = x;

        p->llink = creat();

        p->rlink = creat();

    } else p = NULL;

    return p;

}

void run(NODE *t) { //找到第K个结点(前序)

    if(bre==1) return; //已经找到,退出递归

    if (t) {

        if (num == count) { //递归到第k个结点处

            bre=1; //设置退出变量

            res= t->info;}

        else {

            count++; //计数

            run(t->llink);

            run(t->rlink);}

    }

}

int main() {

    NODE *T;

    printf("请输入一棵树:\n");

    T = creat();

    if (!T) printf("这是一个空的二叉树.");

    else {

        printf("请输入查找的位置节点数:\n");

scanf("%d", &num);

        run(T);

        printf("这个节点是%c", res);

    }

    return 0;

}

 

 

二、实验数据记录和处理

实习题1:

                                               图(1)

 

实习题2:

                                                         图(2)

 

 

实验项目 图

  • 操作方法与实验步骤

实习题二:

#include <stdio.h>

#include <malloc.h>

#include <conio.h>

typedef struct ArcNode{

int adjvex;//该弧指向的顶点

struct ArcNode *nextarc;//下一个弧

int in;//判断是否遍历过该顶点

}ArcNode;

typedef struct VNode{

int data;//顶点信息

ArcNode *firstarc;//指向第一条弧

}VNode,AdjList[20];

typedef struct ALGraph{

AdjList vertices;

int vexnum,arcnum;//顶点数和弧数

int kind;//图的种类标志感觉没什么用

}ALGraph;

int n,i,j;

int m=0;

int s=1;

ALGraph *graph;

void read(ArcNode *firstarc){

int x;

char y;

scanf("%c",&y);

while(y==' ')

scanf("%c",&y);

firstarc->nextarc=NULL;

if(y=='\n'){

return;

}

else{

x=(int)y-48;

ArcNode *t;

t=new ArcNode;

t->nextarc=NULL;

t->in=0;

t->adjvex=x;

firstarc->nextarc=t;

read(firstarc->nextarc);

}

}//输入该顶点出发的弧直到回车

void mkgraph(ALGraph *graph){

char rub;

ArcNode *t;

VNode *list;

scanf("%c",&rub);

for(;s<=n;s++){

printf("%d ",s);

t=new ArcNode;

graph->vertices[s].firstarc=t;

read(graph->vertices[s].firstarc);

}

}//用循环控制输入每个顶点的信息

void run(ArcNode *firstarc){

ArcNode *t;

t=firstarc;

if(t->adjvex==j){

m=1;//若找到则m=1

return;

}

if(t->in==0&&m==0&&t->adjvex>0&&t->adjvex<=n&&graph->vertices[t->adjvex].firstarc->nextarc!=NULL){

t->in=1;

run(graph->vertices[t->adjvex].firstarc->nextarc);

}

if(m==0&&t->nextarc!=NULL)

run(t->nextarc);

}//深度优先搜索

int main(){

graph=new ALGraph;

printf("图中有几个顶点:");

scanf("%d",&n);

printf("请以邻接表方式依次输入各顶点的关系以回车作为结束标志:\n");

mkgraph(graph);

printf("已为您生成图.\n\n请输入您要寻找路径的起点i和终点j(i!=j):");

scanf("%d%d",&i,&j);

while(i==j||i<=0||i>n||j<=0||j>n){

printf("对不起,您输入的数据有误,请重新输入:");

scanf("%d%d",&i,&j);

}//控制输入的数据

run(graph->vertices[i].firstarc->nextarc);

if(m==1)

printf("存在从%d到%d的路径.",i,j);

else

printf("不存在从%d到%d的路径.",i,j);

getch();

}

 

  • 实验数据记录和处理

图1-存在路径的情况

图2-不存在路径的情况

 

实验项目 查找

  • 操作方法与实验步骤

实习题1:编写程序实现下面运算:在二叉排序树中查找关键字为key的记录。

#include <malloc.h>

#include <stdio.h>

#define NULL 0

typedef int KeyType;

typedef struct {

KeyType key;

}ElemType;  //元素类型

//定义二叉树的构成元素

typedef struct BiTNode{

ElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

BiTree searchBST(BiTree bt,KeyType key){

/*在二叉排序树 bt 中查找其关键字等于给定值的结点是否存在,并输出相应信息*/

if (bt==NULL) return NULL;//在排序二叉树中进行递归查找

else if (bt->data.key==key) return bt;

else if (key<bt->data.key) return searchBST(bt->lchild,key);

else return searchBST(bt->rchild,key);

}

void insertBST(BiTree *bt,BiTree s){

/*在二叉排序树中插入一个新结点,即依次插入输入的数*/

if (*bt==NULL) *bt=s;

else if (s->data.key<(*bt)->data.key) insertBST(&((*bt)->lchild),s);

else if (s->data.key>(*bt)->data.key) insertBST(&((*bt)->rchild),s);

}

int main(){

char ch;

KeyType key;

BiTree bt,s;

int i=0;

/*建立一棵二叉排序树,元素从键盘按先序输入,直到输入关键字等于-1为止*/

printf("\n请输入元素(-1:结束):\n");//以-1为结束

scanf("%d",&key);

bt=NULL;

while (key!=-1){   

s=(BiTree)malloc(sizeof(BiTNode));

(s->data).key=key;s->lchild=s->rchild=NULL;

insertBST(&bt,s);

scanf("%d",&key);

}//while

/*二叉排序树的查找,可多次查找,并输出查找的结果*/

do {

printf("\n输入你想要查找的元素:");

scanf("%d",&key);

s=searchBST(bt,key);

if (s!=NULL) printf("\n成功! 这个等价元素是 %d.\n",s->data.key);

else        printf("\n没有找到!\n");

printf("\n是否继续?(y/n):");

scanf("%c",&ch);

ch=getchar();

}

while (ch=='y' || ch=='Y') ;

getchar();

}

实习题2:试将折半查找的算法改写成递归算法。

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

int Search(int* a,int k,int low,int hight){

       int mid;

       if(low>hight){

              return 0;

       }

       mid=(low+hight)/2;

       if(a[mid]==k){

              return (mid+1);

       }

       else if(a[mid]>k){

              return Search(a,k,low,mid-1);

       }

       else{

              return Search(a,k,mid+1,hight);

       }

}

int main(){

       int *p;

       int i,n,key,position;

       printf("你有多少个数据要输入:\n");

       scanf("%d",&n);

       p=(int*)malloc(n*sizeof(int));

       printf("请按非递减序列输入你的数据(整型变量):\n");

       scanf("%d",&p[0]);

       for(i=1;i<n;i++){

              scanf("%d",&p[i]);

              while(p[i]<p[i-1]){

                     printf("你输入的数据不合理,请重新输入:\n");

                     scanf("%d",&p[i]);

              }

       }

       printf("请输入你要查找的数据:\n");

       scanf("%d",&key);

       position=Search(p,key,0,n-1);

       if(position==0){

              printf("没有找到你要查找的数据!\n");

       }

       else{

              printf("你所查找的数据位于原有序表的第%d个位置!\n",position);

       }

       system("pause");

       return 0;

}

 

  • 实验数据记录和处理

实习题1:

图(1)

 

实习题2:

图1-可以查找到数据的情况

图2-没有查找到数据的情况

 

 

实验项目  内排序

  • 操作方法与实验步骤

实习题1:

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

int x; //数据域

struct node *next;

}NODE;

//储存用户输入的数据

void saveData(NODE *head, int n) {

if (n == 0)

return;

int x = 0;

NODE *p;

p = (NODE*)malloc(sizeof(NODE));

head->next = p;

scanf("%d", &x);

p->x = x;

saveData(p, n - 1);

}

//打印数据

void printData(NODE *head, int n) {

NODE *p;

if (n == 0)

return;

p = head->next;

printf("%d ", p->x);

printData(p, n - 1);

}

//直接选择排序

void SelectionSort(NODE *head, int n) {

if (n == 0)

return;

int min;

int i, s = 0;

NODE *p;

NODE *r;

NODE *t;

p = head->next;

r = head->next;

t = head;

min = p->x;

//如果无序序列中有数比最小值还小,则改变s的值

for (i = 0; i < n; i++) {

if (min > p->x) {

min = p->x;

r = p;

s = i;

}

p = p->next;

}

for (i = 0; i < s; i++)

t = t->next;

t->next = r->next;

r->next = head->next;

head->next = r;

SelectionSort(head->next, n - 1); //递归方式进行排序

}

int main() {

int n;

NODE *head;

head = (NODE *)malloc(sizeof(NODE));

printf("请输入数据个数:");

scanf("%d", &n);

printf("请输入%d个数据(空格隔开):", n);

saveData(head, n);

printf("排序后的数据为:\n");

SelectionSort(head, n);

printData(head, n);

printf("\n");

return 0;

}

实习题2:

#include "stdio.h"

#include<stdlib.h>

void Swap(int *a, int *b) {

int c;

c = *a;

*a = *b;

*b = c;

}

void Sort(int *a, int n) {

int h = 0;

for (int i = 1;i<n;i++) {

if (a[i]>=0&&a[h]<0) {

Swap(&a[h], &a[i]);//注意要和swap定义匹配,加上&号

h++;

}

}

}

int main() {

int n;

int *arr;

printf("请输入数的个数:\n");

scanf("%d", &n);

arr = (int*)malloc(n * sizeof(int));  //动态分配数组空间

printf("请输入各关键字:\n");

for (int i = 0;i<n;i++) {

scanf("%d", arr + i);

}

Sort(arr, n);

printf("整序后的结果为:\n");

for (int i = 0;i<n;i++) {

printf("%d ", arr[i]);

}

}

 

  • 实验数据记录和处理

实习题1:

图(1)

 

实习题2:

图(2)

 

 

发布了98 篇原创文章 · 获赞 34 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42133768/article/details/90244466