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

Why is oop better than procedural?

596


#include #include #include #include void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

3230


given a set based questions and 5 questions based on it next data sufficiciency questions 2 and 2/3 english sentence completion with options very easy and 2 synononmys paragraph with 10 questions 10 minutes replace =,-,*,% with -,%,+,* type questions 5 3 questions lik following itssickhere itssickthere itssickhere istsickhere which is nt alike the others very easy

2134


What is object in oop with example?

684


How do you define a class in oop?

610






What type of loop is a for loop?

671


What is abstraction in oop with example?

631


i got a backdoor offer in process global,Bangalore..Can i work with it?

2314


What is the use of oops?

606


What is a class oop?

580


What is coupling in oops?

584


Is react oop?

601


What does and I oop mean?

605


What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?

1642


Can you explain polymorphism?

569