线性表的操作
#include <stdio.h>
#include <stdlib.h>
#define LIST_INTSIZE 50
typedef char DataType;
typedef struct { DataType* elem;
int length;
int listsize;
} SeqList;
int InitSeqList(SeqList* L)
{
L->elem=(DataType *)malloc((LIST_INTSIZE+1)*sizeof(DataType));
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;
}
}
int LenSeqList(SeqList* L)
{ int n=0;
printf("\t\t\t请同学们自己实现程序代码!\n");
return n;
}
int SearchSeqList(SeqList* L, DataType x)
{
printf("\t\t\t请同学们自己实现程序代码!\n");
return 0;
}
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):");
}
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;
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;
}
}
void main()
{
char cmd;
Initialization();
do{
cmd=ReadCommand(cmd);
Interpret(cmd);
if(cmd!='0')
printf("\n\t\t\t请选择命令代码:");
}while(cmd!='0');
}
栈的操作
#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();
}
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");
}
}