Write a program to reverse a linked list?
Answer Posted / ajaypal singh badgujar
#include<iostream>
using namespace std;
class Link;
class Node
{
int value;
Node * next;
friend class Link;
};
class Link
{
Node * start;
public:
Link();
void add();
void display();
void reverse();
};
Link::Link()
{
start=NULL;
}
void Link::add()
{
int value;
Node * node=new Node;
cout<<"Enter the number:";
cin>>node->value;
node->next=NULL;
if(start==NULL)
{
start=node;
}
else
{
Node * temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
void Link::display()
{
Node * temp=start;
while(temp->next!=NULL)
{
cout<<temp->value<<"-->";
temp=temp->next;
}
cout<<temp->value<<"\n";
}
void Link::reverse()
{
Node * temp,* temp1,* temp2;
temp=start;
temp2=NULL;
while(temp)
{
temp1=temp->next;
if(temp1==NULL)
start=temp;
temp->next=temp2;
temp2=temp;
temp=temp1;
}
}
main()
{
int i=0;
Link link;
while(i<6)
{
link.add();
i++;
}
link.display();
link.reverse();
cout<<"======================After reversing\n";
link.display();
}
| Is This Answer Correct ? | 0 Yes | 2 No |
Post New Answer View All Answers
Tell me what are static member functions?
How does com provide language transparency?
What is the hardest coding language to learn?
Will rust take over c++?
What is the difference between an enumeration and a set of pre-processor # defines?
What is the use of cmath in c++?
Are strings immutable in c++?
What do you mean by translation unit?
Give 10 points of differences between C & C++.
What do you mean by vtable and vptr in c++?
When should we use container classes instead of arrays?
What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00
Write about the retrieval of n number of objects during the process of delete[]p?
How would you use the functions sin(), pow(), sqrt()?
What is the best c c++ compiler for windows?