View difference between Paste ID: 5BWZnT9F and reSQjZCW
SHOW: | | - or go back to the newest paste.
1
/* error in remove */
2
3
#include<cstdio>
4
#include<iostream>
5
using namespace std;
6
7
void push(int x);
8
void pop();
9
void search(int x);
10
void update(int oldvalue,int newvalue);
11
void remove(int num);
12
//void insert(int afterwhich,int numbertoinsert);
13
14
typedef struct alvi
15
{
16
    int data;
17
    struct alvi *prev;
18
    struct alvi *next;
19
}node;
20
21
node *head = NULL;
22
node *tail = NULL;
23
24
int main(void)
25
{
26
    int choice,push_number,search_number,remove_number,olds,news;
27
    while(1)
28
    {
29
        printf("Choose : 1 to push,2 to pop,3 to search,4 to update,5 to remove,6 to quit\n");
30
        scanf("%d",&choice);
31
        switch(choice)
32
        {
33
            case 1:
34
                printf("Enter number to push : ");
35
                scanf("%d",&push_number);
36
                push(push_number);
37
                break;
38
            case 2:
39
                pop();
40
                break;
41
            case 3:
42
                printf("Enter number to search : ");
43
                scanf("%d",&search_number);
44
                search(search_number);
45
                break;
46
            case 4:
47
                printf("Enter a old value :");
48
                scanf("%d",&olds);
49
                printf("Enter a new value :");
50
                scanf("%d",&news);
51
                update(olds,news);
52
                break;
53
            case 5:
54
                printf("Enter a number to remove : ");
55
                scanf("%d",&remove_number);
56
                remove(remove_number);
57
                break;
58
            case 6:
59
                return 0;
60
            default:
61
                printf("Wrong input \n");
62
                break;
63
64
        }
65
    }
66
    return 0;
67
}
68
void push(int x)
69
{
70
    node *newNode = new node();
71
    newNode->data = x;
72
    if(head==NULL)
73
    {
74
        cout<<"First node pushed"<<endl;
75
        newNode->prev=NULL;
76
        newNode->next=NULL;
77
        head=newNode;
78
        tail=newNode;
79
    }
80
    else
81
    {
82
        cout<<"other node pushed"<<endl;
83
        head->prev=newNode;
84
        newNode->next=head;
85
        head=newNode;
86
    }
87
}
88
void pop()
89
{
90
    node *cursor;
91
    cursor = tail;
92
    while(cursor!=NULL)
93
    {
94
        cout<<cursor->data<<" ";
95
        cursor=cursor->prev;
96
    }
97
    cout<<endl;
98
}
99
void search(int x)
100
{
101
    node *cursor1;
102
    cursor1 = head;
103
    int flag=0;
104
    int counter = 1;
105
    while(cursor1!=NULL)
106
    {
107
        if(cursor1->data==x)
108
        {
109
            flag=1;
110
            cout<<"found"<<endl;;
111
            cout<<"element number : "<<counter<<endl;
112
            break;
113
        }
114
        cursor1=cursor1->next;
115
        counter++;
116
    }
117
    if(flag==0)
118
    {
119
        cout<<"not found"<<endl;
120
    }
121
}
122
void update(int oldvalue,int newvalue)
123
{
124
    node *cursor2;
125
    cursor2 = head;
126
    int flag=0;
127
    //int counter = 1;
128
    while(cursor2!=NULL)
129
    {
130
        if(cursor2->data==oldvalue)
131
        {
132
            flag=1;
133
            cursor2->data=newvalue;
134
            break;
135
        }
136
        cursor2=cursor2->next;
137
        //counter++;
138
    }
139
    if(flag==0)
140
    {
141
        cout<<"not found"<<endl;
142
    }
143
}
144
void remove(int num)
145
{
146
    node *cursor3;
147
    cursor3 = head;
148
    int flag=0;
149
    //int counter = 1;
150
    while(cursor3!=NULL)
151
    {
152
        if(cursor3->data==num && (cursor3->next==NULL || cursor3->prev==NULL ))
153
        {
154
            flag=2;
155
            if( (cursor3->prev!=NULL && cursor3->next==NULL ) )
156
            {
157
                flag=3;
158
                cursor3->prev=NULL;
159
                break;
160
            }
161
            if( (cursor3->prev==NULL && cursor3->next!=NULL ) )
162
            {
163
                flag=4;
164
                cursor3->next=NULL;
165
                break;
166
            }
167
            if( (cursor3->prev==NULL && cursor3->next==NULL ) )
168
            {
169
                flag=5;
170
            }
171
172
173
174
175
            if(flag==3)
176
            {
177
178
            }
179
            if(flag==4)
180
            {
181
182
            }
183
            if(flag==5)
184
            {
185
186
            }
187
188
189
        }
190
        if(cursor3->data==num && cursor3->next!=NULL && cursor3->prev!=NULL   )
191
        {
192
            flag=1;
193
            (cursor3->prev)->next = cursor3->next;
194
            (cursor3->next)->prev = cursor3->prev;
195
            break;
196
        }
197
        cursor3=cursor3->next;
198
        //counter++;
199
    }
200
    cout<<flag;
201
    if(flag==0)
202
    {
203
        cout<<"not found"<<endl;
204
205
    }
206
}
207
/*
208
void insert(int afterwhich,int numbertoinsert)
209
{
210
    node *cursor4;
211
    cursor4 = head;
212
    int flag=0;
213
    //int counter = 1;
214
    while(cursor4!=NULL)
215
    {
216
        if(cursor4->data==afterwhich)
217
        {
218
            flag=1;
219
220
            break;
221
        }
222
        cursor4=cursor4->next;
223
        //counter++;
224
    }
225
    if(flag==0)
226
    {
227
        cout<<"not found"<<endl;
228
    }
229
}
230
*/