vendredi 28 août 2015

Unable to process linked list deletion

I am trying to work with single linked list and came up with a problem while deleting tail from it. My inputs were 1 1 1 2 2 1 2 3 2 2 then nothing comes after it , for checking i have even put a cout statement , but it's not displaying.What's the problem here ? i am using geany in ubuntu. is there any problem with the compiler ? i have even tried after restarting and also have tried with gcc , so i dont think that there's a problem with compiler .

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

struct node{
    int data;
    node * link;
}*head=NULL,*tail=NULL ,*temp,*temp2;

 void AddElements();

 void DisplayElements();
 void DeleteElements();

int main()
{
int option;
cout<<"Enter your option:"<<endl;
cout<<"1.Add Elements"<<endl;
cout<<"2.Delete Elements"<<endl;
cout<<"3.View Elements"<<endl;
cout<<"4.Exit"<<endl;
cin>>option;
switch(option)
{
    case 1:AddElements();
            break;
    case 2:DeleteElements() ;
            break;
    default: exit(1);
return 0;
}
}
void DisplayElements()
 { temp=head;
     /*cout<<"head"<<head<<endl;
     cout<<"head->data"<<head->data<<endl;
     cout<<"head->link"<<head->link<<endl;*/
     while(temp!=NULL)
        {
     cout<<temp->data;cout<<"--->";
     temp=temp->link; 
        }cout<<"\n";
    main();
 }

void AddElements()
{
    temp=(struct node*)malloc(sizeof(node));
    if(head==NULL)
    {
        head=temp;
        cout<<"Enter first head"<<endl;
        cin>>temp->data;
        head->link=NULL;
        tail=head;
    }
    else if(head!=NULL && head->link==NULL)
    {
        int option2;
        cout<<"1.New Head"<<endl;
        cout<<"2.New Element ? "<<endl;
        cin>>option2;
        if(option2==1)
        {
        cout<<"Enter new head data"<<endl;
        cin>>temp->data;
        temp->link=head;tail=head;
        head=temp;
        }
        else if(option2==2)
        {
            cout<<"Enter new element data"<<endl;
            cin>>temp->data;
            temp->link=NULL;
            head->link=temp;tail=temp;
        }
    }
    else if(head!=NULL && head->link!=NULL)
    {
        int option2;
        cout<<"1.New Head"<<endl;
        cout<<"2.New Element ? "<<endl;
        cin>>option2;
        if(option2==1)
        {
        cout<<"Enter new head data"<<endl;
        cin>>temp->data;
        temp->link=head;
        head=temp;
        }
        else if(option2==2)
        {    
            temp=head;
            temp2=(struct node*)malloc(sizeof(node));
            while(temp->link!=NULL) 
    {
        temp=temp->link;
    }
    temp->link=temp2;
    tail=temp2;
            cout<<"Enter new element data"<<endl;
            cin>>tail->data;


        }
    }
    DisplayElements();
}

void DeleteElements()
{   temp = (struct node*)malloc(sizeof(node));
    int deleteoption;
    cout<<"1.delete head \n";
    cout<<"2.delete tail \n";
    cout<<"3.delete node with position \n";
    cin>>deleteoption;
    cout<<deleteoption;
    if(deleteoption==2)
    {  cout<<"bullshit";
        temp=head;
                    while(temp->link->link!=NULL);
                    {
                    temp=temp->link;        cout<<temp;

                    }
                    temp->link=NULL;
                    DisplayElements();}
}

Aucun commentaire:

Enregistrer un commentaire