/*
Implementation of stack using linklist
Methods included are:
push();
pop();
reverse();
display();
topofstack();
@author: vikas bek
*/
#include<iostream>
using namespace std;
void inputfun();
class stack_ll{
class node{
public:
node *next;
int data;
node(){
data=0;
next=NULL;
}
node(int value,node *ptr){
data=value;
next=ptr;
}
};
public:
node *head,*top;
int capacity;
int no_of_elements;
stack_ll(){
head=top=NULL;
capacity=7;
no_of_elements=0;
}
~stack_ll(){
delete head;
delete top;
}
void push(int value){
if(no_of_elements<capacity){
node *temp=new node(value,NULL);
if(head==0){
head=top=temp;
}else{
node*x=head;
while(x->next!=NULL){
x=x->next;
}
x->next=temp;
top=temp;
}
no_of_elements++;
}else{
cout<<"stack over flow"<<endl;
}
}
int pop(){
int ret;
if(head==NULL){
cout<<"stack underflow "<<endl;
return ret;
}else{
if(head->next==NULL){
ret=top->data;
head=top=NULL;
}else{
ret=top->data;
node*temp=head;
while(temp->next!=top){
temp=temp->next;
}
top=temp;
top->next=NULL;
top->next=NULL;
cout<<"deleted "<<endl;
}
no_of_elements--;
}
return ret;
}
void peek(){
cout<<"top of stack"<<top->data;
}
void display(){
cout<<"The element in the stack: "<<endl;
node *temp=head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
}
stack_ll reverse(stack_ll sl,stack_ll re){
int value=pop();
while(value){
re.push(value);
if(head!=NULL){
value=pop();
}else{
break;
}
}
return re;
// cout<<"re top "<<top->data;
}
};
int main(){
inputfun();
return 0;
}
void inputfun(){
stack_ll re;/*stack used to reverse the default stack*/
stack_ll sl;/*default stack*/
char rep='y';
int ch=0;
do{
int data;
cout<<"menu"<<endl;
cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. display"<<endl;
cout<<"4. top of stack. "<<endl;
cout<<"5. reverse stack. "<<endl;
cout<<"6. display linklist"<<endl;
cout<<"7. To exit \n Enter choice"<<endl;
cout<<"Enter choice"<<endl;
cin>>ch;
switch(ch){
case 1: cout<<"add element in the linklist..."<<endl;
cin>>data;
sl.push(data);
sl.display();
break;
case 2: cout<<"pop: "<<endl;
sl.pop();
sl.display();
break;
case 3: cout<<"display..."<<endl;
sl.display();
break;
case 4: cout<<"at peek "<<endl;
sl.peek();
break;
case 5: cout<<"reverse stack"<<endl;
sl=sl.reverse(re);
break;
case 6: cout<<"display stack "<<endl;
sl.display();
break;
case 7: cout<<"To EXIT!!!!!"<<endl;
exit(0);
default: cout<<"wrong choice: "<<endl;
}
}while(ch!=7);
}
Implementation of stack using linklist
Methods included are:
push();
pop();
reverse();
display();
topofstack();
@author: vikas bek
*/
#include<iostream>
using namespace std;
void inputfun();
class stack_ll{
class node{
public:
node *next;
int data;
node(){
data=0;
next=NULL;
}
node(int value,node *ptr){
data=value;
next=ptr;
}
};
public:
node *head,*top;
int capacity;
int no_of_elements;
stack_ll(){
head=top=NULL;
capacity=7;
no_of_elements=0;
}
~stack_ll(){
delete head;
delete top;
}
void push(int value){
if(no_of_elements<capacity){
node *temp=new node(value,NULL);
if(head==0){
head=top=temp;
}else{
node*x=head;
while(x->next!=NULL){
x=x->next;
}
x->next=temp;
top=temp;
}
no_of_elements++;
}else{
cout<<"stack over flow"<<endl;
}
}
int pop(){
int ret;
if(head==NULL){
cout<<"stack underflow "<<endl;
return ret;
}else{
if(head->next==NULL){
ret=top->data;
head=top=NULL;
}else{
ret=top->data;
node*temp=head;
while(temp->next!=top){
temp=temp->next;
}
top=temp;
top->next=NULL;
top->next=NULL;
cout<<"deleted "<<endl;
}
no_of_elements--;
}
return ret;
}
void peek(){
cout<<"top of stack"<<top->data;
}
void display(){
cout<<"The element in the stack: "<<endl;
node *temp=head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
}
stack_ll reverse(stack_ll sl,stack_ll re){
int value=pop();
while(value){
re.push(value);
if(head!=NULL){
value=pop();
}else{
break;
}
}
return re;
// cout<<"re top "<<top->data;
}
};
int main(){
inputfun();
return 0;
}
void inputfun(){
stack_ll re;/*stack used to reverse the default stack*/
stack_ll sl;/*default stack*/
char rep='y';
int ch=0;
do{
int data;
cout<<"menu"<<endl;
cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. display"<<endl;
cout<<"4. top of stack. "<<endl;
cout<<"5. reverse stack. "<<endl;
cout<<"6. display linklist"<<endl;
cout<<"7. To exit \n Enter choice"<<endl;
cout<<"Enter choice"<<endl;
cin>>ch;
switch(ch){
case 1: cout<<"add element in the linklist..."<<endl;
cin>>data;
sl.push(data);
sl.display();
break;
case 2: cout<<"pop: "<<endl;
sl.pop();
sl.display();
break;
case 3: cout<<"display..."<<endl;
sl.display();
break;
case 4: cout<<"at peek "<<endl;
sl.peek();
break;
case 5: cout<<"reverse stack"<<endl;
sl=sl.reverse(re);
break;
case 6: cout<<"display stack "<<endl;
sl.display();
break;
case 7: cout<<"To EXIT!!!!!"<<endl;
exit(0);
default: cout<<"wrong choice: "<<endl;
}
}while(ch!=7);
}
No comments :
Post a Comment