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
Philippe.Lahaie 42 Posting Whiz in Training
thendrluca
may be some memory leaks
but in general is something like this :)
#include<iostream>
using namespace std;
class linkedlist {
private:
struct node {
int info;
node *next;
} *head, *tail;
public:
linkedlist() {
head = tail = NULL;
}
~linkedlist() {
node* s;
while (head) {
s = head;
head = head->next;
delete s;
}
head = tail = NULL;
}
void append(int);
void addatbeg(int);
void addafter(int, int);
void display();
int count();
};
void linkedlist::append(int num) {
node* temp = new node;
temp->info = num;
temp->next = NULL;
if (tail) {
tail->next = temp;
} else {
head = temp;
}
tail = temp;
}
void linkedlist::addatbeg(int num) {
node* temp = new node;
temp->info = num;
temp->next = NULL;
if (head) {
temp->next = head;
} else {
tail = temp;
}
head = temp;
}
void linkedlist::addafter(int num, int num1) {
node *s;
node *temp = new node;
temp->info = num;
temp->next = NULL;
if (tail) {
s = head;
while(s->info != num1) {
s = s->next;
}
temp->next = s->next;
s->next = temp;
} else {
head = tail = temp;
}
}
void linkedlist::display() {
node *s = head;
cout << endl;
while (s) {
cout << s->info << " ";
s = s->next;
}
cout << endl;
}
int linkedlist::count() {
node *s = head;
int count = 0;
while(s) {
s = s->next;
count++;
}
return count;
}
int main()
{
int ans, n, num, num1;
linkedlist l;
do {
system("cls"); // only in windows platforms
cout << "!!!!!!!!!!!!!MENU!!!!!!!!!!!!" << endl;
cout << "1.Add at beginning" << endl;
cout << "2.Add at last" << endl;
cout << "3.Add after some element" << endl;
cout << "4.Display" << endl;
cout << "5.Count" << endl;
cout << "6.Exit" << endl;
cin >> ans;
switch (ans) {
case 1 :
case 2 :
cout << "How many want to add: "; cin >> n;
break;
case 3 :
cout << "add number : "; cin >> num;
cout << "after number: "; cin >> num1;
l.addafter(num, num1);
break;
case 4 : l.display(); break;
case 5 : cout << endl << l.count() << endl; break;
case 6 : return 0;
}
switch (ans) {
case 1 :
case 2 :
for (int i = 0; i < n; i++) {
cout << "number " << i+1 << ": "; cin >> num;
switch(ans) {
case 1: l.addatbeg(num);
break;
case 2: l.append(num);
break;
}
}
break;
}
system("pause"); // windows platform only
} while (1);
}
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.