Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<process.h>
  4. class node
  5. {
  6.     public:
  7.     int info;
  8.     node *next;
  9. }*start,*newptr,*save,*ptr,*rear;
  10. node *create_new_node(int n)
  11. {
  12.     ptr=new node;
  13.     ptr->info=n;
  14.     ptr->next=NULL;
  15.     return ptr;
  16. }
  17. void Insert(node *np)
  18. {
  19.     if(start==NULL)
  20.         start=rear=np;
  21.     else
  22.     {
  23.         rear->next=np;
  24.         rear=np;
  25.     }
  26. }
  27. void traverse(node *np)
  28. {
  29.     while (np!=NULL)
  30.     {
  31.         cout<<np->info<<" ->";
  32.         np=np->next;
  33.     }
  34.     cout<<"!!!\n";
  35. }
  36. void display(node *np)
  37. {
  38.     while (np!=NULL)
  39.     {
  40.         cout<<np->info<<" -> ";
  41.         np=np->next;
  42.     }
  43.     cout<<"!!!\n";
  44. }
  45. void main()
  46. {
  47.     start=rear=NULL;
  48.     int inf;
  49.     char ch='y';
  50.     while (ch=='y'||ch=='Y')
  51.     {
  52.         cout<<"\nEnter INFOrmation for the new node...";
  53.         cin>>inf;
  54.         newptr=create_new_node(inf);
  55.         if (newptr==NULL)
  56.         {
  57.             cout<<"\nCannot create new node!!!Aborting!!\n";
  58.             getch();
  59.             exit(1);
  60.         }
  61.         Insert(newptr);
  62.         cout<<"\nPress Y to enter more nodes,N to exit...\n";
  63.         cin>>ch;
  64.     }
  65.         cout<<"\nThe list now is: \n";
  66.         traverse(start);
  67.         getch();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement