Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <malloc.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- };
- void addNode (struct node**);
- void printNode (struct node*);
- struct node *deleteNode (struct node *);
- using namespace std;
- int main()
- {
- int menu;
- struct node **top;
- struct node *print = NULL;
- top = &print;
- do{
- do{
- cout << "==========================" << endl;
- cout << "MENU" << endl;
- cout << "1. Add Node " << endl;
- cout << "2. View Node " << endl;
- cout << "3. Delete Node" << endl;
- cout << "4. EXIT" << endl;
- cout << "==========================" << endl;
- cout << "Choose One : ";
- cin >> menu;
- }while(menu < 1 || menu > 5);
- if (menu == 1)
- {
- addNode(top);
- system("cls");
- }
- else if (menu == 2)
- {
- printNode(*top);
- cout<<endl;
- system("pause");
- cout<<endl;
- system("cls");
- }
- else if (menu == 3)
- {
- *top= deleteNode(*top);
- }
- else
- {
- system ("pause");
- break;
- }
- }while (menu != 5);
- return 0;
- }
- void addNode(struct node **top)
- {
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- cout<<"insert Value : ";
- cin>>newNode->data;
- if (*top==NULL)
- {
- newNode->next = NULL;
- }
- else
- {
- newNode->next = *top;
- }
- *top = newNode;
- }
- void printNode(struct node *print)
- {
- while(print != NULL)
- {
- cout<<print->data<<" ";
- print = print->next;
- }
- }
- struct node *deleteNode (struct node *top)
- {
- struct node *temp = top;
- top = top->next;
- free(temp);
- return top;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement