Hai friends,I was developing a code for linked list operations. But while doing deletion, if we give a position except 1, the particular node will be deleted. But if i give position as 1, the node will not be deleted. Please help. I'm posting the code below

#include <iostream.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>

struct node
{
 int info;
 struct node *link;
}*start=NULL;

void push();
void display();
void sort();
void delnode(int);
void search(int value)
{
 int pos=0;
 struct node *_node;
 _node=start;
 if(_node==NULL)
 {
  cout<<"NULL";
  return;
 }
 while(_node!=NULL)
 {
  cout<<_node->info<<"->";
  _node=_node->link;
 }
 cout<<"NULL";
 _node=start; // for searching
 while(_node!=NULL)
 {
  if(_node->info == value)
  {
    cout<<value<<" found at "<< pos;
    getch();
    break;
  }
  else
  {
    _node=_node->link;
    pos++;
  }
 }
}


void main()
{
 clrscr();
 int choice;
 int pos;
 int value;
 char con;
 do
{
 clrscr();
 cout<<"1.Insert"<<endl;
 cout<<"2.Display"<<endl;
 cout<<"3.Sort"<<endl;
 cout<<"4.Delete"<<endl;
 cout<<"5.Search"<<endl;
 cout<<"6.Exit"<<endl;
 cout<<"\nEnter your choice:";
 cin>>choice;
 switch(choice)
 {
  case 1  : push();
        break;
  case 2  : display();
        break;
  case 3  : sort();
        break;
  case 4  : cout<<"Enter the position of node to be deleted : ";
        cin>>pos;
        delnode(pos);
        getch();
        break;
  case 5 : cout<<"Enter the value to be searched : ";
       cin>>value;
       search(value);
       break;
  case 6 : exit(0);
  default : cout<<"\nInvalid choice";
 }
 cout<<"\nDo you want to continue ? ";
 con=getch();
}while(con=='Y' || con=='y');
}


void push()
{
 struct node *linkedlist;
 int value;
 linkedlist=(struct node *) malloc(sizeof(struct node));
 cout<<"Enter the item to be inserted:";
 cin>>value;
 linkedlist->info=value;
 linkedlist->link=start;
 start=linkedlist;
}

void display()
{
  struct node *dll;
  dll=start;
  cout<<endl;
  if(dll == NULL)
  {
   cout<<"NULL";
   return;
  }
  while(dll!= NULL)
  {
    cout<<dll->info<<"->";
    dll=dll->link;
  }
  cout<<"NULL";
}

void delnode(int pos)
{
 struct node *cn,*temp;
 int count=0;
 temp=start;
 cout<<endl;
 while(temp!=NULL)
 {
  temp=temp->link;
  ++count;
 }
 temp=start;
 cn=start;
 count=1;
 while(temp!=NULL)
 {
  if(pos==1 && count==1) // special case : delete first node
  {

   delete temp;
   break;
  }
  else if(pos==count)
  {
   cn->link=temp->link;
   delete temp;
   break;
  }
  else
  {
   cn=temp;
   temp=temp->link;
   ++count;
   continue;
  }
 }
 temp=start; // to display the list after deletion
 if(temp == NULL)
 {
  cout<<"NULL";
  return;
 }
 while(temp!=NULL)
 {
  cout<<temp->info<<"->";
  temp=temp->link;
 }
 cout<<"NULL";

}


/* void sort()
{
 struct node *sll,*ptr;
 sll=start;
 int value;
 while(sll!=NULL)
 {
  value=sll->info;
  sll=sll->link;
  ptr=sll;
  if(value > sll->info)
  {
    int temp;
    temp=value;
    value=sll->info;
    sll->info=temp;
    ptr->info=value;
    ptr=ptr->link;
    ptr->info=sll->info;
    sll=sll->link;
  }
 //sll=sll->link;
 }
 sll=start;
 while(sll!=NULL)
 {
    cout<<sll->info<<"\t";
    sll=sll->link;
    }
} */

Recommended Answers

All 3 Replies

Since I don't feel like trying to understand exactly how your program works, here's a general explanation:

Assuming you are pointing at your first node, and START points at the first node:

Get the NEXT pointer.
Load START with that pointer.
Delete the current node.

I suppose in push() there should be two cases. When start is NULL and when start is non NULL.

void push_Beginning ()
{
  struct node *linkedlist, *save;
  int value;

  linkedlist = new node;

  cout << "Enter the item to be inserted:";
  cin >> value;

  linkedlist->info = value;
  linkedlist->link = NULL;

  //inserting

  if( start == NULL )
    start = linkedlist ;

  else
  {
     save = start ;
     start = linkedlist ;
     linkedlist -> link = save ;
  }

}

See, I'm not using malloc, instead I'm using new. So you don't need alloc.h. Also process.h is not required.

Also your code is not standard. I suppose you are using Turbo C. Avoid using this IDE.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.