中北大学 算法与数据结构实验报告(4)

2019-04-23 22:43

# include # include # define Stack_Length 6 # define OK 1 # define ERROR 0 typedef int SElemType;

typedef struct SNode //define structure LinkStack { SElemType data; struct SNode *next; }SNode,*LinkStack;

int Push_L(LinkStack &top,SElemType e)

{//将元素e插入到栈S中,成为新的栈顶元素 SNode *q;

q=(LinkStack)malloc(sizeof(SNode)); if(!q)

{ cout<

q->data=e;

q->next=top->next; top->next=q; return (OK); } //Push_L() end

void main() //main function { SElemType e,i;

SNode node[Stack_Length]; SNode *p,*top=node;

SElemType array[Stack_Length]={5,8,12,18,30,37}; top=(LinkStack)malloc(sizeof(SNode)); top->next=NULL;

for(i=0;i

{ p=(LinkStack)malloc(sizeof(SNode)); p->data=array[i]; p->next=top->next; top->next=p; }

cout<

cout <

while(p->next)

15

{ p=p->next; cout<data<<\ }

cout<>e;

if(Push_L(top,e)) //call GetTop_L() cout<<\成功!栈顶元素=\ cout<next) { top=top->next; cout<data<<\ }

cout<

4、将元素弹出链式栈算法

# include # include # include # define Stack_Length 6 # define OK 1 # define ERROR 0 typedef int SElemType;

typedef struct SNode //define structure LinkStack { SElemType data; struct SNode *next; }SNode,*LinkStack;

int Pop_L(LinkStack &top,SElemType &e)

{//如果栈S空,返回ERROR;如果栈S不空,删除S的栈顶元素, //用e返回其值,并返回OK SNode *q; if(!top->next)

{ cout<

e=top->next->data;

16

q=top->next;

top->next=q->next; free(q);

return (OK); } //Pop_L() end

void main() //main function { SElemType e,i;

SNode node[Stack_Length]; SNode *p,*top=node;

SElemType array[Stack_Length]={5,8,22,98,70,89}; top=(LinkStack)malloc(sizeof(SNode)); top->next=NULL;

cout<

{ p=(LinkStack)malloc(sizeof(SNode)); p->data=array[i]; p->next=top->next; top->next=p; }

cout <

while(p->next) { p=p->next; cout<data<<\ }

if(Pop_L(top,e)) //弹出后的栈

cout<next) { top=top->next; cout<data<<\ }

cout<

5、取链式栈顶元素算法

17

# include # include # include # define Stack_Length 6 # define OK 1 # define ERROR 0 typedef int SElemType;

typedef struct SNode //define structure LinkStack { SElemType data; struct SNode *next; }SNode,*LinkStack;

int GetTop_L(LinkStack top,SElemType &e)

{//如果栈S空,返回ERROR;如果栈S不空, //用e返回栈S的栈顶元素,并返回OK if(!top->next)

{ cout<

{ e=top->next->data; return (OK); }

} //GetTop_L() end

void main() //main function { SElemType e,i;

SNode node[Stack_Length]; SNode *p,*top=node;

SElemType array[Stack_Length]={15,48,23,18,34,67}; top=(LinkStack)malloc(sizeof(SNode)); top->next=NULL;

for(i=0;i

{ p=(LinkStack)malloc(sizeof(SNode)); p->data=array[i]; p->next=top->next; top->next=p; }

cout<

while(p->next)

18

{ p=p->next; cout<data<<\ \ }

if(GetTop_L(top,e)) //得到栈顶元素 cout<

6、在链式队列尾插入新元素

# include # include # include

# define MAXQSIZE 100 # define OK 1 # define ERROR 0

typedef int QElemType;

typedef struct QNode //define structure QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr;

typedef struct LinkQueue //define structure LinkQueue { QueuePtr front; QueuePtr rear; }LinkQueue;

int InitQueue(LinkQueue &Q) //InitQueue() subfunction { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q.front)

{ cout<

Q.front->next=NULL; return (OK); } //InitQueue() end

int EnQueue(LinkQueue &Q,QElemType e)

19


中北大学 算法与数据结构实验报告(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:考研数学历年真题(1987-2011)年数学一

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: