数据结构--C语言

线性表的操作

#include <stdio.h>
#include <stdlib.h>
#define LIST_INTSIZE 50
typedef char DataType;             /*在此例中数据类型采用字符类型*/


typedef  struct {   DataType*  elem;                    /*线性表的基地址*/
            int  length;                        /*线性表当前的长度*/
            int  listsize;                      /*线性表当前分配的存储容量*/
        } SeqList;   
 //SeqList L;


//初始化线性表
int InitSeqList(SeqList* L)
{ 
    L->elem=(DataType *)malloc((LIST_INTSIZE+1)*sizeof(DataType));  /*数组的0元素没有使用*/
    if (L->elem==NULL)
    {
        printf("\t\t\t内存分配错误\n");
        return 0;
    }
    L->length=0;
    L->listsize=LIST_INTSIZE;  
    printf("\t\t\t内存分配成功\n");
    return 1;
}

//插入
int  InsertSeqList(SeqList* L, int i, DataType x)
{   
    int j; 
    if (L->length==L->listsize)                           /*顺序表已满,不能插入*/
    {   printf("\t\t\t顺序表已满!\n"); 
        return 0; 
    }   
    else
    {
        if(i<1 || i>L->length+1)                   /*检查插入位置的合法性*/             
        {   
            printf("\t\t\t位置不合法!");
            return 0;
        } 
        else
        {
                for(j=L->length;j>=i;j--)             /*顺序移动元素*/
                {     
            L->elem[j+1]=L->elem[j];  
            }
            L->elem[i]=x;                                /*新元素插入*/              
                L->length++;                 /*表长加一*/
            return 1;/*插入成功,返回*/
        }
    }
}

//删除
int DeleteSeqList(SeqList* L, int i)
{
    int  j;
    if(i<1 || i>L->length)                            /*检查删除位置的合法性*/
    { 
        printf ("不存在第i个元素!\n"); 
        return 0; 
    }
    else
    {
        for(j=i;j<L->length;j++)                  /*向上顺序移动元素*/
            L->elem[j]=L->elem[j+1]; 
        L->length--;                             /*表长减一*/
        return 1;                                    /*删除成功*/
     }
}


/* 返回顺序表L的长度*/
int LenSeqList(SeqList* L)
{   int  n=0;
    printf("\t\t\t请同学们自己实现程序代码!\n");

    return  n;
}


/*在线性表中查找值为x的数据元素*/
int SearchSeqList(SeqList* L, DataType x)
{
    printf("\t\t\t请同学们自己实现程序代码!\n");
    return 0;
}


/*返回线性表中的第i个元素的值*/
DataType GetfromSeqList(SeqList* L, int i)
{
    printf("\t\t\t请同学们自己实现程序代码!\n");
    return 0;
}


//输出线性表
void ShowSeqList(SeqList* L)
{   int i;
    printf("\n\t\t\t显示当前线性表的所有元素:");
    if(L->length==0)
        printf("\n\t\t\t线性表为空!\n");
    else
    {
        printf("\n\t\t");
        for(i=1;i<=L->length;i++)
        {   printf("\t%c",L->elem[i]);
        }
    }
}


//将线性表进行逆置处理
//修改算法,使建立线性表时是有序的


void Initialization()
{
  //在屏幕上方显示操作命令清单:
    printf("\n\n\n\n");
    printf("\t\t\t--线 性 顺 序 表--\n");
    printf("\n\t\t\t************************************");
    printf("\n\t\t\t*       1-------顺序表初始化       *");
    printf("\n\t\t\t*       2-------插      入         *");
    printf("\n\t\t\t*       3-------删      除         *");
    printf("\n\t\t\t*       4-------求  表  长         *");
    printf("\n\t\t\t*       5-------按 值 查找         *");
    printf("\n\t\t\t*       6-------读取元素值         *");
    printf("\n\t\t\t*       7-------显示线性表         *");
    printf("\n\t\t\t*       0-------退      出         *");
    printf("\n\t\t\t************************************\n");
    printf("\t\t\t请选择菜单号(0--7):");

}//Initialization

char ReadCommand(char cmd)
{
   //读入操作命令符
   //显示键入操作命令符的提示信息
   do{ cmd=getchar();
    }while(cmd!='1'&&cmd!='2'&&cmd!='3'&&cmd!='4'&&cmd!='5'&&cmd!='6'&&cmd!='7'&&cmd!='0');
   return cmd;
}

void Interpret(char cmd)
{
    int i;
    int j=1;
    SeqList L;
    DataType x;  
    //解释执行操作命令 cmd
  switch(cmd){
   case '1': //初始化;
          InitSeqList(&L);
          break;
  case '2': //插入;
          printf("\n\t\t\t请输入的位置i和数值x(输入格式:i,x):");
          scanf("%d,%c",&i,&x);
          InsertSeqList(&L, i, x);
          ShowSeqList(&L);
          break;
  case '3': //删除
          printf("\n\t\t\t请输入要删除元素的位序:");
          scanf("%d",&i);
          DeleteSeqList(&L, i);
          ShowSeqList(&L);
          break;
  case '4'://求长度
          LenSeqList(&L);
          ShowSeqList(&L);
          break;
  case '5': //检索
          SearchSeqList(&L,x);
          ShowSeqList(&L);
          break;
  case '6'://获取某位置的数据元素
          GetfromSeqList(&L, i);
          ShowSeqList(&L);
          break;
  case '7': //显示线性表
          ShowSeqList(&L);
          break;
  }
}//Interpret

//主函数
void main()
{
  char cmd;
  Initialization(); //初始化
  do{
      cmd=ReadCommand(cmd); //读入一个操作命令符
      Interpret(cmd);  //解释执行操作命令符
      if(cmd!='0')
          printf("\n\t\t\t请选择命令代码:");
  }while(cmd!='0');
}//main

栈的操作

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
typedef   struct node
{   int data;                               /*在此例中栈中的数据类型采用整型*/
    struct  node  *link;
}NODE;
NODE *top;
void Push(int x)                                /*入栈函数*/
{  NODE *p;
   p=(NODE *)malloc(sizeof(NODE));
   p->data=x;
   p->link=top;
   top=p;
}
int Pop ( )                                 /*出栈函数*/
/*将栈顶元素的值出栈,将其值返回*/
{   
    printf("\n\t\t\t请自己实现代码!\n");
    return 0;
}
void ShowStack()                            /*显示栈中所有的元素*/
{   NODE  *p;
    p=top;
    if(p->data==0) printf("\t\t\t 栈中无元素!\n");
    else
    {   printf("\t\t\t 栈中的元素从栈顶到栈底为:");
        while(p->data!=0)
        {   printf("%6d",p->data);
        p=p->link;
        }
    }
}
void  Conversion()                          /*十进制转换为八进制*/
{   int x;
    top->data=0;
    top->link=NULL;
    printf("\n\t\t\t 请输入一个十进制整数: ");
    scanf("%d",&x);
    while(x!=0)
    {   Push(x%8);
        x=x/8;
    }
    printf("\n\t\t\t");
    while(top->data!=0)
    {   x=Pop();
        printf("%d",x);
    }
}
void main()
{   char choice;
    int i=1,j=1;
    int x;

    top=(NODE* )malloc(sizeof(NODE));
    top->data=0;
    top->link=NULL;
    while(i)
    {   printf("\n\n\n\n");
        printf("\t\t\t-链  栈--\n");
        printf("\n\t\t\t************************************");
        printf("\n\t\t\t*       1-------入      栈           *");
        printf("\n\t\t\t*       2-------出      栈           *");
        printf("\n\t\t\t*       3-------显示栈元素           *");
        printf("\n\t\t\t*       4-------进制的转换           *");
        printf("\n\t\t\t*       0-------退      出           *");
        printf("\n\t\t\t************************************\n");
        printf("\t\t\t请选择菜单号(0--4):");
        scanf("%c",&choice);getchar();
        if(choice=='1')
        {   while(j)
            {   printf("\n\t\t\t请输入要入栈的数字(输入0结束):");
                printf("\n\t\t\t");
                scanf("%d",&x);getchar();
                if(x!=0)
                Push(x);                            /*调用入栈函数*/
                else
                j=0;
            }
        }
        else if(choice=='2')
        {   x=Pop();                                /*调用出栈函数*/
            /*printf("\t\t\t 出栈的元素为:%d",x);*/
        }
         else if(choice=='3')
         ShowStack( );                              /*调用显示栈中元素的函数*/
         else if(choice=='4')
         {  Conversion( );                          /*调用进制转换函数*/
            getchar( );
         }
        else if (choice=='0')
        {   i=0;
            printf("\t\t\t程序结束!\n");
        }
        else printf("\n\t\t\t输入错误!请重新输入!\n");
    }
}
#include <stdio.h>
#define STACK_INTSIZE 50                /*设定栈的最大存储空间*/
typedef char DataType;                  /*在此例中栈中的数据类型采用字符类型*/
typedef struct
{   DataType s[STACK_INTSIZE];
    int top;
}Stack;
void Push(Stack* st,DataType x)                         /*入栈函数*/
{   if (st->top== STACK_INTSIZE -1) printf("\n\t\t\t栈已满,不能入栈!");
    else
 {  st->top++;
    st->s[st->top]=x;
 }
}
void Pop(Stack* st)                         /*出栈函数*/
{   DataType x;
    if(st->top==0) printf("\n\t\t\t栈空,不能出栈!\n");
    else
    {   x=st->s[st->top];
        printf("\n\t\t\t出栈元素为:%c\n",x);
        st->top--;
    }
}
DataType ReadTop(Stack* st)                 /*显示栈顶元素的值*/
{   DataType x;
if(st->top==0)
{   printf("\n\t\t\t栈中无元素");
return(0);
}
    else
    x=st->s[st->top];
    return(x);
}
void ShowStack(Stack* st)                       /*显示栈中的所有元素*/
{   int x;
    x=st->top;
    if(x==0) printf("\n\t\t\t栈为空!\n");     
else
    {   printf("\n\t\t\t栈元素为:");
        while(x!=0)
        {   printf("%6c",st->s[x]);
            x--;
        }
}
}

void main()
{   Stack st;
    int i=1;
    char choice;
    DataType x;
    DataType s[STACK_INTSIZE];
    st.top=0;
    while(i)
    {   printf("\n\n\n\n");
        printf("\t\t\t-- 顺 序 栈--\n");
        printf("\n\t\t\t**************************************");
        printf("\n\t\t\t*       1-------入      栈           *");
        printf("\n\t\t\t*       2-------出      栈           *");
        printf("\n\t\t\t*       3-------读栈顶元素           *");
        printf("\n\t\t\t*       4-------显示栈元素           *");
        printf("\n\t\t\t*       0-------退      出           *");
        printf("\n\t\t\t**************************************\n");
        printf("\t\t\t请选择菜单号(0--4):");
        scanf("%c",&choice);getchar( );
        if(choice=='1')
        {   printf("\n\t\t\t请输入要入栈的字符x:");
            scanf("%c",&x); getchar( );
            Push(&st,x);                    /*调用入栈函数*/
        }
        else if(choice=='2')
        Pop(&st);                           /*调用出栈函数*/
        else if(choice=='3')
         {  x=ReadTop(&st);
            printf("\n\t\t\t当前栈顶元素的值是:%c",x);
         }
        else if(choice=='4')
            ShowStack(&st);                 /*调用显示栈顶元素的值的函数*/
        else if (choice=='0')
        {   i=0;
            printf("\n\t\t\t程序结束!\n");
        }
        else printf("\n\t\t\t输入错误!请重新输入!\n");
    }
}

二叉树的操作

#include <stdio.h>
#include <stdlib.h>

typedef  struct  node
{   char data;                       /*此例中二叉树的结点采用字符类型*/
    struct node  *lchild, *rchild;
}NODE;
int count;
NODE *crt_bt_pre( )                  /*按先序遍历序列创建二叉树的二叉链表*/
{   NODE *bt;
    char ch;
    printf("\n\t\t\t");
    scanf("%c",&ch);
    getchar( );
    if(ch==' ')  bt=NULL;
 else
 {   bt=(NODE *)malloc(sizeof(NODE));
     bt->data=ch;
     printf("\n\t\t\t请输入%c结点的左孩子:",bt->data);
     bt->lchild=crt_bt_pre( );
     printf("\n\t\t\t请输入%c结点的右孩子:",bt->data);
     bt->rchild=crt_bt_pre();
 }
 return(bt);
}
void Preorder(NODE* bt)                /*先序遍历二叉树*/
{   if(bt!=NULL)
{    printf("\n\t\t\t %c",bt->data);
        Preorder(bt->lchild);
        Preorder(bt->rchild);
}
}

void Inorder(NODE* bt )                /*中序遍历二叉树*/
{   if(bt!=NULL)
{   Inorder(bt->lchild);
        printf("\n\t\t\t %c",bt->data);
        Inorder(bt->rchild);
}
}
void Postorder(NODE* bt )              /*后序遍历二叉树*/
{   if(bt!=NULL)
    {   Postorder(bt->lchild);
        Postorder(bt->rchild);
        printf("\n\t\t\t %c",bt->data);
    }
}
void CountLeaf(NODE* bt)              /*统计二叉树中叶子结点的个数*/
{   printf("\n\t\t\t请自己实现代码!");
}
int CountNode(NODE* bt)             /*统计二叉树中结点的总数*/
{   if(bt==NULL)
    return 0;
    else
    {   count++;
        CountNode(bt->lchild);
        CountNode(bt->rchild);
        return(count);
    }
}
int TreeDepth(NODE* bt)             /*求二叉树的深度*/
{   int ldep,rdep;
    if(bt==NULL)
      return 0;
    else
    {  ldep=TreeDepth(bt->lchild);
       rdep=TreeDepth(bt->rchild);
       if (ldep>rdep)
       return (ldep+1);
       else
       return (rdep+1);
     }
}
void main()
{   NODE *bt;
    char choice;
    int j=1;
    int x;
    while(j)
    {   printf("\n\n\n\n");
        printf("\t\t\t-二叉树的基本运算--\n");
        printf("\n\t\t\t************************************");
        printf("\n\t\t\t*       1-------建 二  叉 树       *");
        printf("\n\t\t\t*       2-------先 序  遍 历       *");
        printf("\n\t\t\t*       3-------中 序  遍 历       *");
        printf("\n\t\t\t*       4-------后 序  遍 历       *");
        printf("\n\t\t\t*       5-------统计  叶子数       *");
        printf("\n\t\t\t*       6-------统计  结点数       *");
        printf("\n\t\t\t*       7-------求二叉树深度       *");
        printf("\n\t\t\t*       0-------退        出       *");
        printf("\n\t\t\t************************************\n");
        printf("\t\t\t 请选择菜单号(0--7): ");
        scanf("%c",&choice);getchar();
        if(choice=='1')
        {   printf("\n\t\t\t请输入按先序建立二叉树的结点序列:");
            printf("\n\t\t\t说明:逐个输入,输入空格代表后继结点为空,按回车输入下一个结点。");
            printf("\n\t\t\t请输入根结点:");
            bt=crt_bt_pre();                    /*调用创建二叉树的函数*/
            printf("\n\t\t\t二叉树成功建立!\n");
         }
        else if (choice=='2')
        {   printf("\n\t\t\t 该二叉树的先序遍历序列为:");
            Preorder(bt);                       /*调用先序遍历函数*/
        }
        else if (choice=='3')
        {   printf("\n\t\t\t t该二叉树的中序遍历序列为:");
            Inorder(bt);                        /*调用中序遍历函数*/
        }
        else if (choice=='4')
        {   printf("\n\t\t\t该二叉树的后序遍历序列为:");
            Postorder(bt);                      /*调用后序遍历函数*/
        }
        else if (choice=='5')
        {   count=0;
            CountLeaf(bt);                      /*调用统计叶子结点个数的函数*/
            printf("\n\t\t\t该二叉树有%d个叶子结点。\n",count);
        }
        else if (choice=='6')
        {   count=0;
            x=CountNode(bt);                    /*调用统计结点总数的函数*/
            printf("\n\t\t\t该二叉树共有%d个结点。\n",count);
        }
        else if (choice=='7')
        {   x=TreeDepth(bt);                    /*调用求二叉树深度的函数*/
            printf("\n\t\t\t 该二叉树的深度为%d",x);
        }
        else if (choice=='0')
        {   j=0;
            printf("\t\t\t 程序结束!\n");}
        else printf("\n 输入错误,请重新输入!\n");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值