can anyone tell how to make delete function for my program.......and how can i insert more than one value .....ie whenever i insert a value ........the program inserts and then exits.....Thanks in advance!!!!
#include<iostream>
using namespace std;
class linkedlist
{
private:
struct node
{
int info;
node *next;
}*head;
public:
linkedlist()
{
head=NULL;
}
void append();
void addatbeg();
void addafter();
void display();
int count();
// ~linkedlist();
};
void linkedlist::append()
{ int num;
cout<<"Enter info::";
cin>>num;
node *s,*temp;
if(head=NULL)
{
temp=new node;
temp->info=num;
temp->next=NULL;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
s=new node;
s->info=num;
s->next=NULL;
temp->next=s;
}
}
void linkedlist::addatbeg()
{
int num;
cout<<"Enter info::";
cin>>num;
node* temp=new node;
temp->info=num;
temp->next=head;
head=temp;
}
void linkedlist::addafter()
{
int num, num1;
cout<<"Enter data after which value::";
cin>>num1;
cout<<"Enter data::";
cin>>num;
int count=0;
node *s,*temp;
s=head;
while(s->info!=num1)
{
s=s->next;
count++;
}
temp=new node;
temp->info=num;
temp->next=s->next;
s->next=temp;
}
void linkedlist::display()
{
node *temp=head;
cout<<endl;
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->next;
}
}
int linkedlist::count()
{
node *s=head;
int c=0;
while(s)
{
s=s->next;
c++;
}
return c;
}
int main()
{
int ans;
cout<<"!!!!!!!!!!!!!!MeNU!!!!!!!!!!!!"<<endl;
cout<<"1.Add at baginning \n2.Add at last\n3.Add after some element\n4.Display\n5.Count\n6.Exit\n";
cin>>ans;
linkedlist l;
switch(ans)
{
case 1: l.addatbeg();
break;
case 2:l.append();
break;
case 3:l.addafter();
break;
case 4:l.display();
break;
case 5:cout<<l.count()<<endl;
break;
case 6:return 0;
}
}
pinaka
0
Newbie Poster
Recommended Answers
Jump to PostCheck out the Loops section in this tutorial. Surround your switch case with one!
All 2 Replies
Reply to this topic
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.