Stack by linked list
//here is a program that will create a linked stack
#include<iostream>
#include<stdio.h>
using namespace std;
struct linked_lists
{
int data;
struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
node *head;
head=new node;
create(head);
show(head);
int c;
bool x=true;
while(x)
{
cout<<"1 push"<<endl;
cout<<"2 pop"<<endl;
cout<<"3 exit"<<endl;
cin>>c;
switch(c)
{
case 1:
head=push(head);
show(head);
break;
case 2:
head=pop(head);
show(head);
break;
case 3:
x=false;
break;
}
}
return 0;
}
void create(node *p)
{
char c;
scanf("%d%c",&p->data,&c);
if(c=='\n')
{
p->next=NULL;
}
else
{
p->next=new node;
create(p->next);
}
return;
}
void show(node *p)
{
if(p==NULL)
{
cout<<"empty"<<endl;
}
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
node* push(node *p)
{
cout<<"value:";
node *n,*h;
h=p;
n=new node;
cin>>n->data;
n->next=NULL;
if(p==NULL){
p=n;
return p;
}
while(p->next)
{
p=p->next;
}
p->next=n;
return h;
}
node* pop(node *p)
{
node *h;
h=p;
//if(p==NULL)
//{
// return NULL;
//}
if(p->next==NULL)
{
p=p->next;
return p;
}
while(p->next->next!=NULL)
{
p=p->next;
}
delete p->next;
p->next=NULL;
return h;
}
#include<stdio.h>
using namespace std;
struct linked_lists
{
int data;
struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
node *head;
head=new node;
create(head);
show(head);
int c;
bool x=true;
while(x)
{
cout<<"1 push"<<endl;
cout<<"2 pop"<<endl;
cout<<"3 exit"<<endl;
cin>>c;
switch(c)
{
case 1:
head=push(head);
show(head);
break;
case 2:
head=pop(head);
show(head);
break;
case 3:
x=false;
break;
}
}
return 0;
}
void create(node *p)
{
char c;
scanf("%d%c",&p->data,&c);
if(c=='\n')
{
p->next=NULL;
}
else
{
p->next=new node;
create(p->next);
}
return;
}
void show(node *p)
{
if(p==NULL)
{
cout<<"empty"<<endl;
}
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
node* push(node *p)
{
cout<<"value:";
node *n,*h;
h=p;
n=new node;
cin>>n->data;
n->next=NULL;
if(p==NULL){
p=n;
return p;
}
while(p->next)
{
p=p->next;
}
p->next=n;
return h;
}
node* pop(node *p)
{
node *h;
h=p;
//if(p==NULL)
//{
// return NULL;
//}
if(p->next==NULL)
{
p=p->next;
return p;
}
while(p->next->next!=NULL)
{
p=p->next;
}
delete p->next;
p->next=NULL;
return h;
}