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

why freind function takes more parameter than normal member function in c++?

1 Answers   IBM,


features of OOPS

22 Answers   Ness Technologies, Satyam,


Write a program to find out the number of palindromes in a sentence.

1 Answers   TCS,


Can we create object of interface?

0 Answers  


Advantage and disadvantage of routing in telecom sector

0 Answers  






What is the oops and benefits of oops programming?

0 Answers  


any one please tell me the purpose of operator overloading

0 Answers   Amazon,


How do you define social class?

0 Answers  


Can private class be inherited?

0 Answers  


write a program that takes input in digits and display the result in words from 1 to 1000

0 Answers   Wipro,


1. Strong name 2. how to prevent a class from being inherited 3. delegates 4. default modifier for interface 5. default modifier for class 6. base class for exception 7. diff bet trigger and view in sql 8. how to exchange values from one page to another page 9. can multiple catch block ll be executed at same time 10. can u store different data types in an array & array list 11. when we ll use trigger 12. try,catch,finally usage

2 Answers  


What is Hashing and how is it done? Pictorial form?

2 Answers   emc2, Wipro,


Categories