# include
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< cout< if(Push_L(top,e)) //call GetTop_L() cout<<\成功!栈顶元素=\ cout< cout< 4、将元素弹出链式栈算法 # include 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< if(Pop_L(top,e)) //弹出后的栈 cout< cout< 5、取链式栈顶元素算法 17 # include 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< if(GetTop_L(top,e)) //得到栈顶元素 cout< 6、在链式队列尾插入新元素 # 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