SHOW:
|
|
- or go back to the newest paste.
| 1 | /* Easy Stack implementation using singly linked list */ | |
| 2 | #include<iostream> | |
| 3 | #include<cstdio> | |
| 4 | #include<cstdlib> | |
| 5 | #include<cstring> | |
| 6 | using namespace std; | |
| 7 | ||
| 8 | void push(int x); | |
| 9 | void pop(); | |
| 10 | void search(int y); | |
| 11 | ||
| 12 | int nodeCounter = 1; | |
| 13 | ||
| 14 | typedef struct alvi | |
| 15 | {
| |
| 16 | int data; | |
| 17 | struct alvi *next; | |
| 18 | }node; | |
| 19 | node *head = NULL; | |
| 20 | ||
| 21 | int main(void) | |
| 22 | {
| |
| 23 | int choice; | |
| 24 | while(1) | |
| 25 | {
| |
| 26 | printf("Choose 1 for PUSH , 2 for pop , 3 for search , 4 for quit. \t Select : ");
| |
| 27 | scanf("%d",&choice);
| |
| 28 | switch(choice) | |
| 29 | {
| |
| 30 | case 1: | |
| 31 | int number; | |
| 32 | printf("Enter number to push : ");
| |
| 33 | scanf("%d",&number);
| |
| 34 | push(number); | |
| 35 | break; | |
| 36 | case 2: | |
| 37 | pop(); | |
| 38 | break; | |
| 39 | case 3: | |
| 40 | int find; | |
| 41 | printf("Enter number to search : ");
| |
| 42 | scanf("%d",&find);
| |
| 43 | search(find); | |
| 44 | break; | |
| 45 | case 4: | |
| 46 | return 0; | |
| 47 | break; | |
| 48 | default: | |
| 49 | printf("Wrong Selection. Choose correct number \n");
| |
| 50 | break; | |
| 51 | } | |
| 52 | } | |
| 53 | return 0; | |
| 54 | } | |
| 55 | void push(int x) | |
| 56 | {
| |
| 57 | node *newNode=new node(); | |
| 58 | newNode->data=x; | |
| 59 | if(head==NULL) | |
| 60 | {
| |
| 61 | printf("[INITIAL] Node %d pushed \n",nodeCounter);
| |
| 62 | newNode->next=NULL; | |
| 63 | head=newNode; | |
| 64 | nodeCounter++; | |
| 65 | } | |
| 66 | else | |
| 67 | {
| |
| 68 | printf("Node %d pushed \n",nodeCounter);
| |
| 69 | newNode->next=head; | |
| 70 | head=newNode; | |
| 71 | nodeCounter++; | |
| 72 | } | |
| 73 | ||
| 74 | printf("\n");
| |
| 75 | } | |
| 76 | void pop() | |
| 77 | {
| |
| 78 | node *cursor; | |
| 79 | cursor=head; | |
| 80 | while(cursor!=NULL) | |
| 81 | {
| |
| 82 | printf("%d\t",cursor->data);
| |
| 83 | cursor=cursor->next; | |
| 84 | } | |
| 85 | printf("\n");
| |
| 86 | } | |
| 87 | void search(int y) | |
| 88 | {
| |
| 89 | node *pointer; | |
| 90 | pointer=head; | |
| 91 | int flag=0; | |
| 92 | int elementCounter=1; | |
| 93 | while(pointer!=NULL) | |
| 94 | {
| |
| 95 | if(pointer->data==y) | |
| 96 | {
| |
| 97 | flag=1; | |
| 98 | printf("Found.\t");
| |
| 99 | printf("Element number : %d (Pop serial) /",elementCounter);
| |
| 100 | printf(" %d (Push serial) \n",nodeCounter-elementCounter);
| |
| 101 | break; | |
| 102 | } | |
| 103 | pointer=pointer->next; | |
| 104 | elementCounter++; | |
| 105 | } | |
| 106 | if(flag==0) | |
| 107 | {
| |
| 108 | printf("Not Found. \n");
| |
| 109 | } | |
| 110 | printf("\n");
| |
| 111 | } |