数据结构教程第三章实验代码

来源:互联网 发布:淘宝客服手册 编辑:程序博客网 时间:2024/06/05 20:57

3.1

#include<iostream.h>

#include<malloc.h>

#define MaxSize 20
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s);
void DestroyStack(SqStack *&s);
bool StackEmpty(SqStack *&s);
bool Push (SqStack *&s,ElemType e);
bool Pop(SqStack *&s,ElemType &e);
bool GetTop(SqStack *s,ElemType &e);
int GetLength(SqStack *s,int &j);

void OutSqStack(SqStack *&s);

void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *&s)
{
return(s->top==-1);
}
bool Push (SqStack *&s,ElemType e)
{
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
int GetLength(SqStack *s,int &j)
{
if(s->top==-1)
return (j=0);
else
return (j=(s->top+1));
}
 
void OutSqStack(SqStack *&s)
{
int n=s->top;
for(;s->top>-1;s->top--)
cout<<s->data[s->top]<<'\t';
cout<<endl;
s->top=n;
}

void main()
{
SqStack *st;
int i;
ElemType e,a,b,c,d;
InitStack(st);
StackEmpty(st);
Push (st,'a');
Push (st,'b');
Push (st,'c');
Push (st,'d');
StackEmpty(st);
GetLength(st,i);
cout<<"zhan de chang du shi:"<<endl<<i<<endl;
cout<<"cong zhan ding dao zhan di de yuan shu :"<<endl;
OutSqStack(st);
cout<<"chu zhan de xu lie shi:"<<endl;
OutSqStack(st);
StackEmpty(st);
Pop(st,d);
Pop(st,c);
Pop(st,b);
GetTop(st,e);
Pop(st,a);
cout<<e<<endl;
DestroyStack(st);
StackEmpty(st);
}


3.2#include<iostream.h>

#include<malloc.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
int length;
}LiStack;
void InitStack(LiStack *&s);
void DestroyStack(LiStack *&s);
void display(LiStack *&s,int j);
bool StackEmpty(LiStack *s);
void Push(LiStack *&s,ElemType e);
bool Pop(LiStack *&s,ElemType &e);
bool GetTop(LiStack *s,ElemType &e);
int Getlength(LiStack *s,int &j);



void InitStack(LiStack *&s)
{
s=(LiStack *)malloc(sizeof(LiStack));
s->next=NULL;
s->length=0;
}
void DestroyStack(LiStack *&s)
{
LiStack *p=s,*q=s->next;
while(q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}


bool StackEmpty(LiStack *s)
{
return(s->next==NULL);
}


void Push(LiStack *&s,ElemType e)
{
LiStack *p;
p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=s->next;
s->next=p;
s->length++;
}
 bool Pop(LiStack *&s,ElemType &e)


{
LiStack *p;
if(s->next==NULL)
return false;
p=s->next;
e=p->data;
s->next=p->next;
s->length--;
free(p);
return true;
}


bool GetTop(LiStack *s,ElemType &e)
{
if(s->next==NULL)
return false;
e=s->next->data;
return true;
}


int Getlength(LiStack *s,int &j)
{
return(j=s->length);
}


void display(LiStack *&s,int j)
{
LiStack *p=s;
for(int i=0;i<j;i++)
{
p=p->next;
cout<<p->data<<"\t";
}
cout<<endl;
}

void main()
{
LiStack *st;
int i;
ElemType e;
InitStack(st);
StackEmpty(st);
Push(st,'a');
Push(st,'b');
Push(st,'c');
Push(st,'d');
Push(st,'e');
    cout<<StackEmpty(st)<<endl;
cout<<"链表的长度是:"<<endl;
cout<<Getlength(st,i)<<endl;
cout<<"栈顶到栈低的元素是:"<<endl;
display(st,i);
cout<<"出链栈序列为:"<<endl;
display(st,i);
cout<<StackEmpty(st)<<endl;
DestroyStack(st);
}



3.3

include<iostream>

#include<malloc.h>
typedef char ElemType;
#define MaxSize 10
using namespace std;
typedef struct
{
 ElemType data[MaxSize];
 int front,rear;
}sqQueue;
void InitQueue(sqQueue *&q);
void DestroyQueue(sqQueue *&q);
bool QueueEmpty(sqQueue *q);
bool enQueue(sqQueue *&q,ElemType e);
bool deQueue(sqQueue *&q,ElemType &e);

void InitQueue(sqQueue *&q)
{
q=(sqQueue *)malloc(sizeof(sqQueue));
q->front=q->rear=-1;
}
void DestroyQueue(sqQueue *&q)
{
free(q);
}


bool QueueEmpty(sqQueue *q)
{
return(q->front==q->rear);
}


bool enQueue(sqQueue *&q,ElemType e)
{
if(q->rear==MaxSize-1)
return false;
q->rear++;
q->data[q->rear]=e;
return true;
}
bool deQueue(sqQueue *&q,ElemType &e)
{
if(q->front==q->rear)
return false;
q->front++;
e=q->data[q->front];
cout<<e<<endl;
return true;

}



void main()
{
sqQueue *st;
int j;
ElemType e;
    InitQueue(st);
QueueEmpty(st);
    enQueue(st,'a');
    enQueue(st,'b');
enQueue(st,'c');
cout<<"出队一个元素,并输出该元素;"<<endl;
deQueue(st,e);
cout<<"队列q的元素个数"<<endl;
cout<<st->rear-st->front<<endl;
enQueue(st,'d');
    enQueue(st,'e');
enQueue(st,'f');
cout<<"队列q的元素个数:"<<endl;
cout<<st->rear-st->front<<endl;
    cout<<"出队序列:"<<endl;
j=st->rear-st->front;
for(int i=0;i<j;i++)
{
st->front++;
cout<<st->data[st->front]<<endl;

}
DestroyQueue(st);
}


3.4

#include<iostream>

#include<malloc.h>
typedef char ElemType;
using namespace std;
typedef struct qnode
{
ElemType data;
struct qnode *next;
}QNode;
typedef struct 
{
QNode *front;
QNode *rear;
int j;
}LiQueue;
void InitQueue(LiQueue *&q);
void DestroyQueue(LiQueue *&q);
bool QueueEempty(LiQueue *q);
void enQueue(LiQueue *&q,ElemType e);
bool deQueue(LiQueue *&q,ElemType &e);

void InitQueue(LiQueue *&q)
{
q=(LiQueue *)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
q->j=0;
}
void DestroyQueue(LiQueue *&q)
{
QNode *p=q->front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;r=p->next;
}
}
free(p);
free(q);
}
bool QueueEempty(LiQueue *q)
{
return (q->rear==NULL);
}
void enQueue(LiQueue *&q,ElemType e)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL)
q->front=q->rear=p;
else
{
q->rear->next=p;
q->rear=p;
}
q->j++;
}


bool deQueue(LiQueue *&q,ElemType &e)
{
QNode *t;
if(q->rear==NULL)
return false;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
q->j--;
cout<<e<<endl;
free(t);
return true;
}

void main()
{
LiQueue *st;
ElemType e;
    InitQueue(st);
QueueEempty(st);
    enQueue(st, 'a');
    enQueue(st, 'b');
    enQueue(st, 'c');
cout<<"出队一个元素,输出该元素:"<<endl;
    deQueue(st,e);
cout<<"输出链列q元素的个数:"<<endl;
cout<<st->j<<endl;
enQueue(st, 'd');
    enQueue(st, 'e');
    enQueue(st, 'f');
cout<<"输出链列q的元素个数:"<<endl;
cout<<st->j<<endl;
cout<<"输出出队序列:"<<endl;
    for(int i=0;i<st->j;i++)
{
cout<<st->front->data<<'\t';
st->front=st->front->next;
}
cout<<endl;
DestroyQueue(st);
}



原创粉丝点击