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

Write a program in c++ to read two floating point numbers and find their sum and average.

2 Answers  


what is the sylabus for priliminaries?

0 Answers  


in the following, A D B G E C F Each of the seven digits from 0,1,2,3,4,5,6,7,8,9 is: a)Represented by a different letter in abov fig: b)Positioned in the fig abov so tht A*B*C,B*G*E,D*E*F are equal. wch does g represents? C

1 Answers   IonIdea,


what about you? wahat is your object? introduce your self?

1 Answers   Ajmal Perfumes, TCS,


sir plz send me a set of questions that been frequently held in written examination during campus selection.

0 Answers   TCS,






What is use of overloading?

0 Answers  


for example A,B,C,D are class all the 4 class contain one method who() but the method who() implementaion is differnet among each class. the relation among the 4 class are A is base class and is inherited by B and C.and from this two B and C where D is inherited. the question is i want to display the output who() method in all the classes(A,B,C,D)the output of who() method is diferrent amond all the class(A,B,C,D) ------A------ virtuval who(print a) override | | who(print b) B C override who(print c) | | -------D------ override who(print d)

2 Answers  


What is encapsulation with real life example?

0 Answers  


What is function overloading?,describe it with the example.

5 Answers  


what do you mean by static member variable?

2 Answers  


What is the types of inheritance?

0 Answers  


what is the difference between inter class and abstract class...?

0 Answers  


Categories