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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Who invented oop?

659


What is constructor overloading in oop?

607


What is overloading in oops?

598


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

1994


What is interface? When and where is it used?

1666






What is abstract class in oops?

603


What do you mean by overloading?

586


write knight tour problem which is present in datastructure

2168


String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?

1944


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1411


What is solid in oops?

617


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

1692


This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line preceded by a line number. Print the line number right-adjusted in a field of 3 spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen. Otherwise, allow default printer or screen output behavior if the line is too long (i.e., wrap or truncate). A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 72-character line.

1643


What is overriding in oops?

606


c++ program to swap the objects of two different classes

1767