View difference between Paste ID: U3ZEzgBH and 4nAZQvG8
SHOW: | | - or go back to the newest paste.
1
#include<iostream>
2
using namespace std;
3
int element=0;
4
void push(int x);
5
void pop();
6
void search(int x);
7
8
typedef struct alvi
9
{
10
    int data;
11
    struct alvi *prev;
12
    struct alvi *next;
13
14
}node;
15
node *head = NULL;
16
node *tail = NULL;
17
18
int main(void)
19
{
20
    int choice,push_number,search_number,remove_number,olds,news;
21
    while(1)
22
    {
23
        cout<<"Choose : 1 to push,2 to pop,3 to search,4 to quit"<<endl;
24
        cin>>choice;
25
        switch(choice)
26
        {
27
            case 1:
28
                cout<<"Enter number to push : ";
29
                cin>>push_number;
30
                push(push_number);
31
                break;
32
            case 2:
33
                pop();
34
                break;
35
            case 3:
36
                cout<<"Enter number to search : ";
37
                cin>>search_number;
38
                search(search_number);
39
                break;
40
            case 4:
41
                return 0;
42
            default:
43
                cout<<"Wrong input"<<endl;
44
                break;
45
        }
46
    }
47
    return 0;
48
}
49
void push(int x)
50
{
51
    node *newNode = new node();
52
    newNode->data = x;
53
    if(head==NULL)
54
    {
55
        cout<<"First node pushed"<<endl;
56
        newNode->prev=NULL;
57
        newNode->next=NULL;
58
        head=newNode;
59
        tail=newNode;
60
    }
61
    else
62
    {
63
        cout<<"other node pushed"<<endl;
64
        head->prev=newNode;
65
        newNode->next=head;
66
        head=newNode;
67
        //make the list circular
68
        newNode->prev=tail;
69
        tail->next=head;
70
    }
71
    element++;
72
    //head->next=tail;
73
}
74
void pop()
75
{
76
    node *cursor;
77
    cursor = tail;
78
    while(cursor!=NULL && cursor!=head)
79
    {
80
        cout<<cursor->data<<" ";
81
        cursor=cursor->prev;
82
    }
83
    cout<<head->data;
84
    cout<<endl;
85
}
86
void search(int x)
87
{
88
int b[element],j=0,k=0,flag=0;
89
    node *cursor;
90
    cursor = tail;
91
    if(head->data==x )
92
    {
93
        b[element-1]=x;
94
        flag=1;
95
    }
96
    while(cursor!=NULL && cursor!=head)
97
    {
98
        b[j]=cursor->data;
99
        j++;
100
        //cout<<cursor->data<<" ";
101
        cursor=cursor->prev;
102
    }
103
    while(k<element+1)
104
    {
105
        cout<<b[k]<<" ";
106
        if(b[k]==x)
107
        {
108
            flag=1;
109
            break;
110
        }
111
        k++;
112
    }
113
114
    if(flag==1)
115
    {
116
        cout<<"found"<<endl;
117
    }
118
    else
119
    {
120
        cout<<"not found"<<endl;
121
    }
122
}