Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


design class for linked list and include
constructor,destructor,insert option.
struct node
{
int node;
struct node &ptr;
}



design class for linked list and include constructor,destructor,insert option. struct node { in..

Answer / ayushi rastogi

#include<iostream.h>

struct node
{
int data; // D.O.B. would be better
node *ptr;// Pointer to next node
};

node *start_ptr = NULL;
node *current; // Used to move along the list
int option = 0;
class linklist
{
public:
linklist()
{
start_ptr=NULL;
}
~linklist()
{
start_ptr=NULL;
}

public:
void add_node_at_end()
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the data: ";
cin >> temp->data;
temp->ptr = NULL;

// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->ptr != NULL)
{ temp2 = temp2->ptr;
// Move to next link in chain
}
temp2->ptr = temp;
}
}
public:
void display_list()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Data : " << temp->data << " ";

if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->ptr;

}
cout << "End of list!" << endl;
}
}
};
void main()
{
linklist l;
start_ptr = NULL;
do
{
l.display_list();
cout << endl;
cout << "Please select an option : " << endl;
cout << "0. Exit the program." << endl;
cout << "1. Add a node to the end of the list."
<< endl;
cout << "2. Display." << endl;

cout << endl << " >> ";
cin >> option;

switch (option)
{
case 1 : l.add_node_at_end(); break;
case 2 : l.display_list(); break;
}
}
while (option != 0);
}

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More OOPS Interview Questions

Prepare me a program for the animation of train

0 Answers  


How to calculate the age from the date of birth by using the program?

2 Answers   Accenture,


namespace is working on which compiler?

3 Answers  


Why is there no multiple inheritance?

0 Answers  


what is the difference between class to class type conversion and copy constructor ?

2 Answers  






Contrast OOP and SOA. What are tenets of each?

1 Answers   Siebel Systems, Wipro,


How do you answer polymorphism?

0 Answers  


how do u initialize the constant variables

5 Answers   IBM, Siemens,


INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

0 Answers  


What are the different forms of polymorphism??

8 Answers   Mantaq, NUC, PCS,


different types of castings

3 Answers   Siemens,


What is byval and byref? What are differences between them?

0 Answers   HCL, Wipro,






Categories