September 24, 2012 / IST / Language, Software Development.

In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.

Singly-linked-list.svg
A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list.
/*Q-15->WAP in C++ To Delete a Node From a Linked List Having User Given Data 'X' in it*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
class lnklst                   //class defination
{
  private:
	   struct Node
	   {  int info;            //node structure
	      Node *next;
	   }*first,*ptr,*track,*temp,*x,*y;
  public:
	   lnklst()
	   {first=ptr=track=temp=NULL;}
	   void insert(int);
	   void delnode(int);         //function declarations
	   void display();
};
void lnklst::insert(int n)
{
  ptr=new Node;
     if(ptr==NULL)
       { cout<<"\nNode Cannot Be Created";
	 getch();
	 exit(1);
       }
  ptr->info=n;
  ptr->next=NULL;

  if(first==NULL)                 //defination of function insert
    first=track=ptr;
  else
     {
       track->next=ptr;
       track=track->next;
     }
   cout<<"\nData Inserted In The List!!!";
}
void lnklst::delnode(int dn)
{ int flag=0;
  temp=first;                    //defination of delnode
  if(first==NULL)
      cout<<"\nList is Empty";
  while(temp!=NULL)
  {   y=temp->next;
     if(first->info==dn)   //when data to be deleted is in the first node
       {first=first->next;
	flag=1;
       }
     else if(y->info==dn)
       {  if(y->next==NULL)    //when the data is at the last node
	    {track=temp;
	     temp->next=NULL;
	     }
	  else
	     temp->next=y->next;   //when data is in between the list

	  flag=1;
	}

     temp=temp->next;
  }
  if(flag==0)
     cout<<"\n\nSorry!!!Data Not Found In The List.";
  else
     cout<<"\nData Found And Deleted Sucessfully!!!";
}
void lnklst::display()
{
 x=first;
   if(first==NULL)
     cout<<"\nThe List is Empty";
  while(x!=NULL)
   {                              //fn to display the list
     cout<<x->info<<"  ";
     x=x->next;
   }
}
void main()              //main
{
  lnklst ob;
  char choice;
  int ch,data,d;
  do
   {clrscr();
    cout<<"\n\n\tDELETION OF A NODE FROM A SINGLY LINKED LIST HAVING DATA 'X'";
    cout<<"\n\t----------------------------------------------------------";
    cout<<"\n\n1.INSERT NODE";
    cout<<"\n2.DELETE NODE";           //menu
    cout<<"\n3.DISPLAY";
    cout<<"\n4.EXIT";
    cout<<"\n\nWhich Operation Do You Want To Perform(1-4):";
    cin>>ch;
	    switch(ch)
		  {
		    case 1: cout<<"\nEnter the data to be inserted:";
			    cin>>data;
			    ob.insert(data);
			    break;
		    case 2: cout<<"\nEnter the Data To Be Deleted:";
			    cin>>d;
			    ob.delnode(d);     //calling fns
			    break;
		    case 3: ob.display();
			    break;
		    case 4: exit(0);
		    default: cout<<"\nPlease Enter a Valid Choice(1-4):";
		  }
    cout<<"\n\nDo You Want To Continue(Y/N):";
    cin>>choice;

  }while(choice=='Y' || choice=='y');
  cout<<"\n\n\t\tPROGRAM DEVELOPRED BY:"
      <<"\n\n\t\t\tTSF CORPORATION LTD."
      <<"\n\t\t\tCopyright  2008"
      <<"\n\t\t\twww.tsfcl.com";
  getch();
}                   //end of the program