//deleting a node from a linked list

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<conio.h>
using namespace std;

struct linked_lists{
    int data;
    struct linked_lists *next;
};

typedef struct linked_lists node;

void create(node *p);

void show(node *p);

node *remove(node *p);

node *insert(node *p);

void search(node *p);

int main()
{
    node *head;
    head=new (node);
    create(head);
    show(head);
    head=remove(head);
    show(head);
    return 0;
}

void create(node *p)
{
    char c;

    scanf("%d%c",&p->data,&c);
    if(c=='\n')
    {
        p->next=NULL;
        system("cls");
    }
    else{
        p->next=new node;
        create(p->next);
    }
    return;
}

void show(node *p)
{
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return;
}




node *remove(node *p)
{
    node *copy;
    copy=p;
    int f=1,position;
    cout<<"which position?\n";
    cin>>position;
    system("cls");
    while(p)
    {
             if(position==1)
    {
        node *n;
        n=new (node);
        n=p->next;
        delete p;
        p=n;
        return p;
    }
    if(f+1==position)
    {
        node *n;
        n=new (node);
        n=p->next->next;
        delete p->next;
        p->next=n;
        return copy;

    }
    f++;
    p=p->next;
    }
}

No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...