iam having problem in displaying the elements present in singly linkllist.here is my code below plz check the whole code and pointout the errors and give their solution plzzzzz.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

    struct node
    {
      int data;
      node *ptr;
   };
  node *first,*p,*nn;
class list
{
public:
	void inslast(int);
	void insbeg(int);
	void ins_after(int,int);
	void ins_bef(int,int);
	void delelement(int);
	void delbeg();
	void dellast();
	void disp();
	void search(int);
   list()
   {first=NULL;}

};

void list::inslast(int x)
{

    nn=new node;
    nn->data=x;
    p=first;
    while(p->ptr!=NULL)
    { p=p->ptr;}
    p->ptr=nn;
    nn->ptr=NULL;
    p=nn;
	cout<<"\n\t\t Inserted successfully at the end..\n ";
	disp();

}

void list:: insbeg(int x)
{

	nn=new node;
	nn->data=x;
	nn->ptr=first;
   first=nn;
   //p=first;
	cout<<"\n\t\t  Inserted successfully at the begining..\n";
	disp();
}


void list::delelement(int x)
{
   node *q;
   p=first;
   while(p->data==x)
    {
     q=p;
      p=p->ptr;
    }
      q->ptr=p->ptr;
      cout<<"\n\t\t Successfully deleted..\n";

}

void list:: delbeg()
{
	cout<<" \n\t\t The list before deletion: ";
	disp();
   p=first;
   p=p->ptr;
   first=p;
   cout<<"\n\t\t Successfully deleted..\n";
}


void list:: dellast()
{
	cout<<"\n\t\t  The list before deletion: ";
	disp();
	node *q;
   p=first;
   while(p->ptr!=NULL)
    {
     q=p;
     p=p->ptr;
    }

   q->ptr=NULL;
   cout<<"\n\t\t Successfully deleted..\n";
}



void list::disp()
{

	p=first;
	while(p->ptr!=NULL)
	{
		cout<<"	"<<p->data;
		p=p->ptr;
	}

}

void list::ins_after(int value,int pos)
{

    nn=new node;
    nn->data=value;
    p=first;
    while(p->data!=pos)
	{ p=p->ptr; }
	  nn->ptr=p->ptr;
    p->ptr=nn;
	cout<<" \n\t\t  Inserted successfully at the position.."<<pos;
	disp();
}

 void list::ins_bef(int value,int pos)
  {
   node *q;
   nn=new node;
   nn->data=value;
   p=first;
   while(p->data!=NULL)
   {
    q=p;
    p=p->ptr;
   }
   nn->ptr=p;
   q->ptr=nn;
   cout<<"\n\t\t  Inserted successfully at the position.."<<pos;
	disp();
  }

void list::search(int value)
{
   p=first;
   while(p->ptr!=NULL)
    {
     if(p->data==value)
     {
      cout<<"\n\t\tYes the element "<<value<<" is found";
     }
      p=p->ptr;
    }

	cout<<"\n\t\t Element "<<value<<" not found";  


void main()
{

list l;
int ch,val,req,sel;
do
{
	clrscr();
   cout<<"\t\t..:::****~WELCOME~..:::****\n";
	cout<<" \n\n\t\tFucntions in this single Link list..\n";
	cout<<"\t\t 1.Insertion   2.Deletion   3.Display   4.Search   5.Exit";
	cout<<" \n\t\t Enter choice for fucntion: ";
	cin>>ch;

	switch(ch)
	{
	case 1:
		cout<<"\n\t\t 1.Insertion at begining:";
      cout<<" \n\t\t 2.Insertion at the end: ";
		cout<<"\n\t\t 3.Insertion after required node:";
     	cout<<"\n\t\t 4.Insertion before required node:";
		cout<<" \n\t\t Enter ur choice:";
		cin>>sel;
		cout<<" \n\t\tEnter the value to insert in linklist:";
		cin>>val;
		switch(sel)
		{
			case 1:
				l.insbeg(val);
				break;
			case 2:
				l.inslast(val);
				break;
			case 3:
				cout<<" \n\t\tEnter the value after which the node to be inserted:";
				cin>>req;
				l.ins_after(val,req);
				break;
         case 4:
           	cout<<" \n\t\t Enter the value before which the node to be inserted:";
				cin>>req;
				l.ins_bef(val,req);

			default:
				cout<<"\n\t\t Invalid choice: ";

		}
	break;

	case 2:
		cout<<" \n\t\t 1.Delete the first element.";
      cout<<" \n\t\t 2.Delete the last element.";
		cout<<"  \n\t\t 3.Enter the element to delete from the list";
		cout<<"  Enter ur choice:";
		cin>>sel;
		switch(sel)
		{
			case 1:
				l.delbeg();
				cout<<" \n\t\tThe list after deletion: ";
            l.disp();
				break;
			case 2:
				l.dellast();
				cout<<"\n\t\t The list after deletion:";
            l.disp();
				break;
			case 3:
				l.disp();
				cout<<"\n\t\t Enter the element to delete :	";
				cin>>val;
				l.delelement(val);
				cout<<"\n\t\t The list after deletion:";
            l.disp();
				break;

			default:
				cout<<" \n\t\tThe option is invalid...";
				break;
		}
	break;

	case 3:
		l.disp();
		break;

	case 4:
		l.disp();
		cout<<"\n\t\t Enter the element to search:";
		cin>>val;
		l.search(val);
		break;

	case 5:
		exit(1);

	default:
		cout<<" \n\t\t The option selected is invalid... ";

	}
	}
while(1);
getch();

}

Recommended Answers

All 2 Replies

>>while(1);
That's an infinite loop

>>void main()
Not a good thing to do. Here's why.

The function just above main is missiong a closing }

void list::inslast(int x)
{

nn=new node;
nn->data=x;
p=first;
while(p->ptr!=NULL)
{ p=p->ptr;}

Write a code to handle p == NULL

void list::delelement(int x)
{
node *q;
p=first;
while(p->data==x)
{
q=p;
p=p->ptr;
}
q->ptr=p->ptr;
cout<<"\n\t\t Successfully deleted..\n";

}

while ( p->data != x ) should be used. x is what you dont want right.

while deleting you should use 'delete p ;'

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.