Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. void addNode (struct node**);
  12. void printNode (struct node*);
  13. struct node *deleteNode (struct node *);
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19.     int menu;
  20.     struct node **top;
  21.     struct node *print = NULL;
  22.  
  23.     top = &print;
  24.  
  25.     do{
  26.         do{
  27.             cout << "==========================" << endl;
  28.             cout << "MENU" << endl;
  29.             cout << "1. Add Node " << endl;
  30.             cout << "2. View Node " << endl;
  31.             cout << "3. Delete Node" << endl;
  32.             cout << "4. EXIT" << endl;
  33.             cout << "==========================" << endl;
  34.             cout << "Choose One : ";
  35.             cin >> menu;
  36.         }while(menu < 1 || menu > 5);
  37.  
  38.         if (menu == 1)
  39.         {
  40.             addNode(top);
  41.             system("cls");
  42.         }
  43.         else if (menu == 2)
  44.         {
  45.             printNode(*top);
  46.             cout<<endl;
  47.             system("pause");
  48.             cout<<endl;
  49.             system("cls");
  50.         }
  51.         else if (menu == 3)
  52.         {
  53.            *top= deleteNode(*top);
  54.         }
  55.         else
  56.         {
  57.             system ("pause");
  58.             break;
  59.         }
  60.     }while (menu != 5);
  61.     return 0;
  62. }
  63.  
  64. void addNode(struct node **top)
  65. {
  66.     struct node *newNode;
  67.  
  68.     newNode = (struct node*)malloc(sizeof(struct node));
  69.  
  70.     cout<<"insert Value : ";
  71.     cin>>newNode->data;
  72.  
  73.     if (*top==NULL)
  74.     {
  75.         newNode->next = NULL;
  76.     }
  77.     else
  78.     {
  79.         newNode->next = *top;
  80.     }
  81.     *top = newNode;
  82. }
  83.  
  84. void printNode(struct node *print)
  85. {
  86.     while(print != NULL)
  87.     {
  88.         cout<<print->data<<" ";
  89.         print = print->next;
  90.     }
  91. }
  92.  
  93. struct node *deleteNode (struct node *top)
  94. {
  95.     struct node *temp = top;
  96.     top = top->next;
  97.     free(temp);
  98.     return top;
  99. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement