《数据结构实验》

一:

题目:栈的基本操作

实验算法描述:

1.建立顺序栈,并在顺序栈上实现入栈和出栈操作(验证性内容)。
2.建立链栈,并在链栈上实现入栈和出栈操作(设计性内容)。
3.实现“回文检测”求解问题(应用性设计内容)。

代码:

#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
 
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
 
typedef int Status; 
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
typedef int SElemType;
#define STACK_INIT_SIZE 20 /* 存储空间初始分配量*/
#define STACKINCREMENT 10 /* 存储空间分配增量*/
typedef struct SqStack
{
    SElemType *base; /* 在栈构造之前和销毁之后,base的值为NULL */
    SElemType *top; /* 栈顶指针*/
    int stacksize; /* 当前已分配的存储空间,以元素为单位*/
}SqStack; /* 顺序栈*/
 
Status InitStack(SqStack &S)
{ /* 构造一个空栈S */
    S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)
        exit(OVERFLOW); /* 存储分配失败*/
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
 
Status DestroyStack(SqStack &S)
{ /* 销毁栈S,S不再存在*/
    free(S.base);
    S.base=NULL;
    S.top=NULL;
    S.stacksize=0;
    return OK;
}
 
Status ClearStack(SqStack &S)
{ /* 把S置为空栈*/
    S.top=S.base;
    return OK;
}
 
Status StackEmpty(SqStack S)
{ /* 若栈S为空栈,则返回TRUE,否则返回FALSE */
    if(S.top==S.base)
        return TRUE;
    else
        return FALSE;
}
 
int StackLength(SqStack S)
{ /* 返回S的元素个数,即栈的长度*/
    return S.top-S.base;
}
 
Status GetTop(SqStack S,SElemType &e)
{ /* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
    if(S.top>S.base)
    {
        e=*(S.top-1);
        return OK;
    }
    else
        return ERROR;
}
 
Status Push(SqStack &S,SElemType e)
{
    if(S.top - S.base >= S.stacksize) 
    {
        S.base = (SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base) 
            exit (OVERFLOW); 
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *(S.top)=e;
    S.top++;
    return OK;
}
 
Status Pop(SqStack &S,SElemType &e)

//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR 
    if(S.top==S.base)
        return ERROR;
    e=*(S.top-1);
    --S.top;
    return OK;
}
 
Status StackTraverse(SqStack S)
{ /* 从栈底到栈顶依次访问栈中每个元素*/
    SElemType *p; 
    p=S.base;
    while(p<S.top)
    {
        printf("%d ",*p);
        p++;
    }
    printf("\n");
    return OK;
}
 
int main()
{
    int j;
    SqStack s;
    SElemType e;
    if(InitStack(s)==OK)
        for(j=1;j<=12;j++)
            Push(s,j);
    j=StackLength(s);
    printf("栈中元素个数为:%d\n",j);
    printf("栈中元素依次为:");
    StackTraverse(s);
    Pop(s,e);
    printf("弹出的栈顶元素e=%d\n",e);
    printf("栈空否:%d(1:空0:否)\n",StackEmpty(s));
    GetTop(s,e);
    printf("栈顶元素e=%d 栈的长度为%d\n",e,StackLength(s));
    ClearStack(s);
    j=StackEmpty(s);
    printf("清空栈后,栈空否:%d(1:空0:否)\n",j);
    DestroyStack(s);
    printf("销毁栈后,s.top=%u s.base=%u s.stacksize=%d\n",s.top,s.base, s.stacksize);
    return 0;
}

 

结果:

 

 

二:

题目:二叉树排序树的建立

实验算法描述:

  二叉排序树的建立比普通二叉树多了一点就是在建立的过程中需要对插入进来的数据进行大小的判断,但是就是因为多了这么一点,所以不能在递归的过程中去输入数据并插入,而是应该先输入数据,再将数据插入。

    插入的过程中要将数据与树的根结点的值开始比较知道找到合适的位置插入,所以在插入数据的这一部分代码中可以使用递归算法。

代码:

#include"stdio.h" 
#include"malloc.h"

#define MAX_NODE 100

typedef struct node
{
    int data;
    struct node *LChild;
    struct node *RChild;
}BST;
void CreateBST( BST *T, int arr[], int n );
void  InorderSearch( BST *T );

int main()
{
    int arr[] = { 18, 3, 7, 20, 14, 19, 2 };
    BST *T = ( BST * )malloc( sizeof( BST ) );
    CreateBST( T, arr, 7 );
    InorderSearch( T );
    return 0;
}
void CreateBST( BST *T, int arr[], int n ) 
{
    BST *s, *p, *q; 
    int i; 
    T->data = arr[0]; 
    T->LChild = NULL; 
    T->RChild = NULL; 
    for( i = 1; i < n; i++ ) 
    {
        s = (BST *)malloc( sizeof(BST) ); 
        s->data   = arr[i];
        s->LChild = NULL;
        s->RChild = NULL;
        p = T;
        while( p != NULL )
        {
            if( s->data == p->data )
            {
                printf( "%d existed.\n", s->data );
                q = NULL;
                break; 
            }
            q = p;
            if( s->data < p->data )
            {
                p = p->LChild;
            }
            else if( s->data > p->data )
            {
                p = p->RChild;            
            }
        } 
        if( q != NULL && s->data < q->data )
        {
            q->LChild = s;
        }
        else if( q != NULL && s->data > q->data )
        {
            q->RChild = s;
        }    
    }
}

void  InorderSearch( BST *T )
{
    if( T != NULL )
    {
        InorderSearch( T->LChild );
        printf( "%5d", T->data );
        InorderSearch( T->RChild );
    } 
}
 

结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值