This is a singlylinked list code.This shows no error but
while execution it shows singlylinkedlist.exe
has encountered a problem and needs to close
thanks in advance
#include "singlylinkedlist.h"
#include <iostream>
using namespace std;
class singlylinkedlist //class for linkedlist
{
private:
struct node //struct daclares the abstract data and link
{
public:
int data;
node* next;
}*head,*tail; //instance for struct
public:
singlylinkedlist(); //constructor
void add_as_first(int num); //adds a node at the start of linkedlist
void add_after(int count,int num); //adds a node in the middle of linkedlist
void add_as_last(int num); //adds a node at the end of linkedlist
int del_first(); //deletes a node at start and returns the value
int del_last(); //deletes a node at the end and returns the value
int del_this_node(int num); //deletes a particular node
int no_of_nodes(); //counts the no of nodes in the linkedlist
void disp_node_values(); //displays all the node values
~singlylinkedlist();
};
singlylinkedlist::singlylinkedlist()
{
head=NULL;
tail=NULL;
}
void singlylinkedlist::add_as_first(int num)
{
node *q;
if(head=NULL)
{
head=new node;
head->data=num;
head->next=NULL;
tail=head;
}
else
{
q=new node;
q->data=num;
q->next=head;
head=q;
}
}
void singlylinkedlist::add_after(int count,int num)
{
int i;
node* q,* t;
if(head=NULL)
{
cout<<"Queue is empty\n";
}
else
{
q=head;
for(i=0;i<count;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"There are less than "<<count<<" nodes in the list\n";
}
}
t=new node;
t->data=num;
t->next=q->next;
q->next=t;
}
}
void singlylinkedlist::add_as_last(int num)
{
node* q,* t;
if(head=NULL)
{
cout<<"Queue is empty\n";
}
else
{
q=head;
while(q!=NULL)
{
q=q->next;
}
t=new node;
t->data=num;
q->next=t;
t->next=NULL;
tail=t;
}
}
int singlylinkedlist::del_first()
{
int num;
node* q;
if(head=NULL)
{
cout<<"Queue is empty\n";
return NULL;
}
else
{
num=head->data;
q=head;
delete head;
q=q->next;
head=q;
return num;
}
}
int singlylinkedlist::del_last()
{
int num;
node* q;
if(head=NULL)
{
cout<<"Queue is empty\n";
return NULL;
}
else
{
q->next=tail;
num=tail->data;
delete tail;
q->next=NULL;
tail=q;
return num;
}
}
int singlylinkedlist::del_this_node(int num)
{
node* q,* s,* t;
if(head=NULL)
{
cout<<"Queue is empty\n";
return NULL;
}
else
{
q=head;
while(q->data==num)
{
q=q->next;
}
s->next=q;
t=q->next;
s->next=q->next;
delete q;
return num;
}
}
int singlylinkedlist::no_of_nodes()
{
node* q;
int i=0;
q=head;
while(q!=NULL)
{
i++;
q=q->next;
}
return i;
}
void singlylinkedlist::disp_node_values()
{
int i=1;
node* q;
if(head==NULL)
{
cout<<"Queue is empty\n";
}
else
{
cout<<"Node 1 value is: "<<head->data<<"\n";
q=head;
while(q!=NULL)
{
i++;
q=q->next;
cout<<"node "<<i<<" value is: "<<q<<"\n";
}
}
}
singlylinkedlist::~singlylinkedlist()
{
}
int main()
{
singlylinkedlist A;
cout<<"No of Elements: "<<A.no_of_nodes()<<"\n";
A.add_as_first(1);
cout<<"1\n";
A.add_after(1,2);
cout<<"1\n";
A.add_as_last(3);
A.disp_node_values();
A.del_first();
A.del_last();
A.del_this_node(2);
A.no_of_nodes();
return 0;
}