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