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