[img]http://my.iteye.com/admin/picture/123306[/img]
#include"stdio.h"
#include"malloc.h"
typedef struct Node
{
int data;
Node *next;
}Node;
void InitStack(Node *&top)
{
top = (Node *)malloc(sizeof(Node));
top->next = NULL;
}
void createStack(Node *top)
{
printf("Input data,input -1 to end:");
int n;
Node *q;
scanf("%d",&n);
while(n!= -1)
{
q = (Node *)malloc(sizeof(Node));
q->data = n;
q->next = top->next;
top->next = q;
scanf("%d",&n);
}
}
void display(Node *top)
{
Node *r;
r = top->next;
printf("The List: ");
while(r!=NULL)
{
printf("%d ",r->data);
r = r->next;
}
printf("\n");
}
void Popstack(Node *top,int x)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = top->next;
top ->next= p;
}
void Pushstack(Node *top, int &x)
{
Node *r;
if(top->next!=NULL)
{
r = top->next;
x = r->data;
top->next = r->next;
free(r);
}
}
void main()
{
Node *top;
InitStack(top);
createStack(top);
display(top);
Popstack(top,9);
display(top);
int x;
Pushstack(top,x);
printf("The delete the element is %d: \n",x);
display(top);
}