Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- typedef struct node Node;
- using namespace std;
- struct node {
- int data;
- Node *next;
- };
- Node *func1(Node *head,int item)
- {
- Node *loc;
- Node *locp;
- Node *current= head;
- int c=0;
- if(head==NULL)
- {
- return head;
- }
- while(current!=NULL)
- {
- if(current->data ==item)
- {
- loc=current->next;
- c=1;
- break;
- }
- locp=current;
- loc=current->next;
- current= current->next;
- }
- if(c==1)
- {
- locp->next=loc;
- return head;
- }
- }
- Node *func2(Node *head, int n)
- {
- int c=1;
- Node *loc;
- Node *locp;
- Node *current= head;
- while(current!=NULL)
- {
- if(c==n)
- {
- loc=current->next;
- break;
- }
- locp=current;
- current=current->next;
- c++;
- }
- locp->next= loc;
- return head;
- }
- Node *func3(Node *head,Node *locp, Node *loc)
- {
- locp->next=loc->next;
- return head;
- }
- Node show( Node *head)
- {
- Node *current= head;
- while(current!=NULL)
- {
- cout<<current->data<<" ";
- current=current->next;
- }
- cout<<endl;
- }
- int main()
- {
- int n=4;
- Node *ar[n+1];
- for(int i=0;i<n;i++)
- {
- ar[i]= (Node*)malloc(sizeof(Node));
- ar[i]->data=i+1;
- }
- Node *head =ar[0];
- for(int i=0;i<n-1;i++)
- {
- ar[i]->next= ar[i+1];
- }
- ar[n-1]->next= NULL;
- cout<<"[Suppose linked list have these values : 1,2,3,4 ]"<<endl;
- cout<<"Enter any option from below: "<<endl;
- cout<<"Press 1: Delete a node which contains a certain value"<<endl;
- cout<<"Press 2: Delete the nth node you want to delete"<<endl;
- cout<<"Press 3: Delete a node by entering two location i.e. previous node & the location of the node"<<endl;
- int opt;
- cin>>opt;
- if(opt==1)
- {
- int v;
- cout<<"Enter the value you want to delete"<<endl;
- cin>>v;
- func1(head,v);
- show(head);
- }
- else if (opt == 2)
- {
- int v;
- cout<<"Enter the nth node number"<<endl;
- cin>>v;
- func2(head, v);
- show(head);
- }
- else if (opt ==3)
- {
- Node *loc= (Node*)malloc(sizeof(Node));
- Node *locp=(Node*)malloc(sizeof(Node));
- cout<<"Enter LOC: ";
- }
- else
- {
- cout<<"Option out of Range"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement