.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Pushing up linked stack

Posted By:Nikhil Kumar       Posted Date: February 28, 2010    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  

This c code showing you push up in linked stack...
 

#include
#include
#include
#include
struct node{          int info;
                                              node*next;
                               }              *top,*newptr,*save,*ptr;
node *newnode(int);          
                      ////to create new node
void push(node*);
void display(node*);
void main()
{
clrscr();
top=NULL;
int inf;char ch='y';
while(ch=='y')
{
cout<<"ENTER INFI IN NEW NODE:";
cin>>inf;
newptr=newnode(inf);
push(newptr);
cout<<"PRESS Y TO ENTER NEW NODE:";
cin>>ch;
}
do{cout<<"THE STACK NOW IS:";
display(top);getch();
}
while(ch=='y');
}
node*newnode(int n)
{
ptr=new node;
ptr->info=n;ptr->next=NULL;
return ptr;
}
void push(node*np)
{
if (top==NULL)top=np;
else{save=top;top=np;
np->next=save;
}
}
void display(node*np)
{
while (np!=NULL)
{
cout<info<<"->";
np=np->next;
}
cout<<"!!!\n" ;
}
 
OUTPUT 
 
ENTER INFI IN NEW NODE:12
PRESS Y TO ENTER NEW NODE:y
ENTER INFI IN NEW NODE:23
PRESS Y TO ENTER NEW NODE:y
ENTER INFI IN NEW NODE:12
PRESS Y TO ENTER NEW NODE:y
ENTER INFI IN NEW NODE:65
PRESS Y TO ENTER NEW NODE:y
ENTER INFI IN NEW NODE:44
PRESS Y TO ENTER NEW NODE:n
THE STACK NOW IS:44->65->12->23->12->!!!
 
 Thanks !!!


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend