⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 push_l.cpp

📁 数据结构代码
💻 CPP
字号:
//Push_L.cpp
//This program is to push LinkStack
# include <malloc.h>
# include <iostream.h>
# include <conio.h>
# 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)	//Push_L() sub-function
{   SNode *q;
    q=(LinkStack)malloc(sizeof(SNode));
    if(!q)
    {   cout<<endl<<"Overfolow ! Allocate space failure ! ";
	return (ERROR);
    }
    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<Stack_Length;i++)
    {   p=(LinkStack)malloc(sizeof(SNode));
	p->data=array[i];
	p->next=top->next;
	top->next=p;
    }
    cout<<endl<<endl<<"Push_L.cpp";
    cout<<endl<<"==========";
    cout <<endl<<endl<<"The old LinkStack is (top to bottom) : ";	//output
    p=top;
    while(p->next)
    {   p=p->next;
	cout<<p->data<<" ";
    }
    cout<<endl<<"Please input the data to push : ";
    cin>>e;
    if(Push_L(top,e))			//call GetTop_L()
       cout<<"Success ! The top element of LinkStack="<<e;
    cout<<endl<<"The new LinkStack is :                 ";
    while(top->next)
    {   top=top->next;
	cout<<top->data<<" ";
    }
    cout<<endl<<endl<<"...OK!...";
    getch();
} //main() end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -