NBA - Boston Celtics

Wednesday 4 September 2013

linked list,stack and queue(1)

INSERTION  IN  A  LIST:


#include<iostream.h>
#include<process.h>
struct node
{
int info;
node*next;
}*save,*start,*newptr,*ptr;
node*create_new_node(int);
void insert_beg(node*);
void display(node*);
int main()
{
save=NULL;
int inf;
char ch;
ch='y';
while(ch=='Y'||ch=='y')
{
cout<<"enter information of new node : ";
cin>>inf;
newptr=create_new_node(inf);
insert_beg(newptr);
cout<<"now the list is : ";
display(start);
cout<<"want to enter nodes (y/n) : ";
cin>>ch;
}
return 0;
}
node*create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert_beg(node*np)
{
if(start==NULL)
start=np;
else
{
save=start;
start=np;
np->next=save;
}
}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"!!!\n";
}


OUTPUT


No comments:

Post a Comment