Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *head=NULL,*temp;
- void delete_position(int z)
- {
- struct node *pre,*cur;
- int i=2;
- pre=head;
- cur=head->next;
- if(z==1)
- {
- head=cur;
- delete(pre);
- }
- else
- {
- while(i!=z && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- i++;
- }
- if(i!=z && cur->next==NULL)
- {
- cout<<"Position was not found in the list"<<endl;
- }
- else
- {
- pre->next=cur->next;
- delete(cur);
- }
- }
- }
- void display()
- {
- temp=head;
- cout<<endl;
- while(temp!=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->next;
- }
- cout << endl;
- }
- int main()
- {
- int n,i,x;
- struct node *s;
- s=(struct node*)malloc(sizeof(struct node));
- cout<<"Enter how many link list u want:"<<endl;
- cin>>n;
- for(i=1; i<=n; i++)
- {
- struct node *newnode;
- newnode=(struct node*) malloc (sizeof(struct node));
- cin>>newnode->data;
- newnode->next=NULL;
- if(head==NULL)
- {
- head=newnode;
- s=head;
- }
- else
- {
- s->next=newnode;
- s=newnode;
- }
- }
- cout<<"Enter a position of a note which u want to delete:"<<endl;
- int z;
- cin>>z;
- delete_position(z);
- display();
- }
Add Comment
Please, Sign In to add comment