Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* error in remove */
- #include<cstdio>
- #include<iostream>
- using namespace std;
- void push(int x);
- void pop();
- void search(int x);
- void update(int oldvalue,int newvalue);
- void remove(int num);
- //void insert(int afterwhich,int numbertoinsert);
- typedef struct alvi
- {
- int data;
- struct alvi *prev;
- struct alvi *next;
- }node;
- node *head = NULL;
- node *tail = NULL;
- int main(void)
- {
- int choice,push_number,search_number,remove_number,olds,news;
- while(1)
- {
- printf("Choose : 1 to push,2 to pop,3 to search,4 to update,5 to remove,6 to quit\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- printf("Enter number to push : ");
- scanf("%d",&push_number);
- push(push_number);
- break;
- case 2:
- pop();
- break;
- case 3:
- printf("Enter number to search : ");
- scanf("%d",&search_number);
- search(search_number);
- break;
- case 4:
- printf("Enter a old value :");
- scanf("%d",&olds);
- printf("Enter a new value :");
- scanf("%d",&news);
- update(olds,news);
- break;
- case 5:
- printf("Enter a number to remove : ");
- scanf("%d",&remove_number);
- remove(remove_number);
- break;
- case 6:
- return 0;
- default:
- printf("Wrong input \n");
- break;
- }
- }
- return 0;
- }
- void push(int x)
- {
- node *newNode = new node();
- newNode->data = x;
- if(head==NULL)
- {
- cout<<"First node pushed"<<endl;
- newNode->prev=NULL;
- newNode->next=NULL;
- head=newNode;
- tail=newNode;
- }
- else
- {
- cout<<"other node pushed"<<endl;
- head->prev=newNode;
- newNode->next=head;
- head=newNode;
- }
- }
- void pop()
- {
- node *cursor;
- cursor = tail;
- while(cursor!=NULL)
- {
- cout<<cursor->data<<" ";
- cursor=cursor->prev;
- }
- cout<<endl;
- }
- void search(int x)
- {
- node *cursor1;
- cursor1 = head;
- int flag=0;
- int counter = 1;
- while(cursor1!=NULL)
- {
- if(cursor1->data==x)
- {
- flag=1;
- cout<<"found"<<endl;;
- cout<<"element number : "<<counter<<endl;
- break;
- }
- cursor1=cursor1->next;
- counter++;
- }
- if(flag==0)
- {
- cout<<"not found"<<endl;
- }
- }
- void update(int oldvalue,int newvalue)
- {
- node *cursor2;
- cursor2 = head;
- int flag=0;
- //int counter = 1;
- while(cursor2!=NULL)
- {
- if(cursor2->data==oldvalue)
- {
- flag=1;
- cursor2->data=newvalue;
- break;
- }
- cursor2=cursor2->next;
- //counter++;
- }
- if(flag==0)
- {
- cout<<"not found"<<endl;
- }
- }
- void remove(int num)
- {
- node *cursor3;
- cursor3 = head;
- int flag=0;
- //int counter = 1;
- while(cursor3!=NULL)
- {
- if(cursor3->data==num && (cursor3->next==NULL || cursor3->prev==NULL ))
- {
- flag=2;
- if( (cursor3->prev!=NULL && cursor3->next==NULL ) )
- {
- flag=3;
- cursor3->prev=NULL;
- break;
- }
- if( (cursor3->prev==NULL && cursor3->next!=NULL ) )
- {
- flag=4;
- cursor3->next=NULL;
- break;
- }
- if( (cursor3->prev==NULL && cursor3->next==NULL ) )
- {
- flag=5;
- }
- if(flag==3)
- {
- }
- if(flag==4)
- {
- }
- if(flag==5)
- {
- }
- }
- if(cursor3->data==num && cursor3->next!=NULL && cursor3->prev!=NULL )
- {
- flag=1;
- (cursor3->prev)->next = cursor3->next;
- (cursor3->next)->prev = cursor3->prev;
- break;
- }
- cursor3=cursor3->next;
- //counter++;
- }
- cout<<flag;
- if(flag==0)
- {
- cout<<"not found"<<endl;
- }
- }
- /*
- void insert(int afterwhich,int numbertoinsert)
- {
- node *cursor4;
- cursor4 = head;
- int flag=0;
- //int counter = 1;
- while(cursor4!=NULL)
- {
- if(cursor4->data==afterwhich)
- {
- flag=1;
- break;
- }
- cursor4=cursor4->next;
- //counter++;
- }
- if(flag==0)
- {
- cout<<"not found"<<endl;
- }
- }
- */
Add Comment
Please, Sign In to add comment