implementation of stacks using linked lists in c++

implementation of stacks using linked lists in c++

how to implementation of stacks using linked lists in c++

View Answers

January 7, 2012 at 11:19 AM

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node{
    int data;
    struct node *link;
    };
    struct node *top=NULL,*temp;
    void main(){
    int choice,data; 
    clrscr();  
    while(1){
    printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
    printf("\nEnter ur choice:");    
    scanf("%d",&choice);   
    switch(choice){  
        case 1:  
        temp=(struct node *)malloc(sizeof(struct node)); 
        printf("Enter a node data :"); 
        scanf("%d",&data); 
        temp->data=data;  
        temp->link=top; 
        top=temp;   
        break;    
        case 2:   
        if(top!=NULL){
        printf("The poped element is %d",top->data); 
        top=top->link; 
        }          
        else{
        printf("\nStack Underflow");
        }
        break;
        case 3:
        temp=top;
        if(temp==NULL){
        printf("\nStack is empty\n");
        }
        while(temp!=NULL){
        printf("->%d->",temp->data);
        temp=temp->link;
        }
        break;
        case 4:
        exit(0);
        }
      }
    }

January 7, 2012 at 11:21 AM

#include<stdio.h>
#include<malloc.h>

#define maxsize 10
void push();
void pop();
void display();

struct node{
int info;
struct node *link;
}
*start=NULL, *new,*temp,*p;
typedef struct node N;
main(){
int ch,a;
do{
printf("\t\t\tLinked stack");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch){
case 1:
push();
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
exit(0);
default:

printf("\nInvalid choice");
break;
}
}
while(ch<=3);
}
void push(){
new=(N*)malloc(sizeof(N));
printf("\nEnter the item : ");
scanf("%d",&new->info);
new->link=NULL;
if(start==NULL)
start=new;
else{
p=start;
while(p->link!=NULL)
p=p->link;
p->link=new;
}
}
void pop(){
if(start==NULL)
printf("\nStack is empty");
else if(start->link==NULL){
printf("\nThe deleted element is : %d",start->info);
free(start);
start=NULL;
}
else{
p=start;
while(p->link!=NULL){
temp=p;
p=p->link;
}
printf("\nDeleted element is : %d\n", p->info);
temp->link=NULL;
free(p);
}
}
void display(){
if(start==NULL)
printf("\nStack is empty");
else{
printf("\nThe elements are : ");
p=start;
while(p!=NULL){
printf("%d",p->info);
p=p->link;
}
printf("\n");
}
}

Ads









Related Tutorials/Questions & Answers:
implementation of stacks using linked lists in c++
implementation of stacks using linked lists in c++  how to implementation of stacks using linked lists in c++   #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node{ int data
Linked Lists in C
Linked Lists in C  Hi all, I have a project to be implemented in C... help. Thank you in advance. Write a C program for managing car parts store. The treatment is done in a linked list. The application works on the command line
Advertisements
linked lists
linked lists  write a program to create a circular linked list in java and perform operations on it?   import java.util.*; public class CircularLinkedList<E> { private Entry<E> head; private Entry<
Linked list implementation
Linked list implementation  How to create linkedlist by using array in java? and also How to manipulate
stack using linked list
stack using linked list  how to implement stack using linked list
implementation object in C
implementation object in C  how to create and implement an object in C
c++ garbage collection implementation
c++ garbage collection implementation  How to implement the garbage collection program in C
lists
lists  how to implement lists by using array
Multiplication of two polynomials using linked list in java
Multiplication of two polynomials using linked list in java  I am doing a program "Multiplication of two polynomials using linked list in java... for polynomial linked list import java.util.Scanner; class Node { public int
write a java pogram to add elements to linked list using keyboard and display it
write a java pogram to add elements to linked list using keyboard and display it  write a java pogram to add elements to linked list using keyboard and display
Calculate Entropy using C++
Calculate Entropy using C++  # include <iostream> # include <cmath> using namespace std; int main() { float S0,S1,S2,S3; float Hs,Hs3; float
How to create a Student data base using Linked List in java
How to create a Student data base using Linked List in java  I want a program by using linked list in java. The data stored must... Record2 sandhya 22 5apr By using linked list I hve
How to print the following output using c program
How to print the following output using c program  1) 4 3 4 2 3 4 1 2 3 4 2) A B C D E F G H I J
Online exam using c# and SQL server
Online exam using c# and SQL server  hello, i'm making the (window based)online examination system using C#.net and SQL SERVER. my database is stored on server. my question is: if during the examination server gets failed
linked list
linked list  program for single linked list
Linked list
Linked list  what is difference btw linked list in datastructure and linked list in java
Linked List
Linked List  public class Computer{ private int computerNumber... in the following logical linked list represents Computer Number. 76(head) 98 54...? to ?66?. The segment should also display the new contents of the linked list
Making 301 redirection by using <c:redirect>
Making 301 redirection by using   How can i set the status as 301 when using tag? When i use , I am able to redirect to the desired URL (here, Google page) but I get 302 as the status of redirect. I need this status as 301. Any
linked list
linked list  hi i have basal problem what is the linked list
linked list
linked list   how to write a program using a linked list, that will prompt a user to enter 10 names and display them in reverse   import java.util.*; class LinkedListExample { public static void main(String[] args
linked list
linked list  Hi i have a problem with linked list ! how and where i can use linked list? please give me some example.   Please visit...://www.roseindia.net/java/beginners/linked-list-demo.shtml
How ro convert char into string using recursive function in c#??
How ro convert char into string using recursive function in c#??  This coding is for java...but i want this in c# How ro convert char into string using recursive function in c#?? char [] ch={'H','E','L','L','o'}; TO ans
ModuleNotFoundError: No module named 'stacks'
ModuleNotFoundError: No module named 'stacks'  Hi, My Python... 'stacks' How to remove the ModuleNotFoundError: No module named 'stacks'... to install padas library. You can install stacks python with following command
linked list
linked list   how to add student and mark and number from file in linked list and print them also how to make search function plz can help me sooon
C++
C++  dear sir How to create windows form application for login screen using C++? USER Name -TESTADMIN Password -testuser
C++
C++  Trace the bubble sort using the following integers,which represent the elements in an array. 5,7,3,8,6,7,3
linked list
linked list  Data Structures An English institute has a different... program to manage the registration details for the institute. 1. Use a linked list to manage the details of all registered students. a. Create your own linked list
C++
C++  How can i write this in dev c
C++
C++  How can i write this in dev c
c#
c#  how to find out the size of the BMP image in C
ModuleNotFoundError: No module named 'djangocms-stacks'
ModuleNotFoundError: No module named 'djangocms-stacks'  Hi, My... named 'djangocms-stacks' How to remove the ModuleNotFoundError: No module named 'djangocms-stacks' error? Thanks   Hi, In your

Ads