ReverseLinkedList
#include
using namespace std;
class LinkedList
{
private:
struct Node
{
int data;
Node *next;
};
Node *root;
public:
LinkedList()
{
root = NULL;
}
void insert(int);
void Print();
void Reverse();
};
void LinkedList::insert(int d)
{
Node* current = new Node;
current->data = d;
current->next = NULL;
Node* parent;
parent = root;
Node* temp;
if(root == NULL)
{
root = current;
}
else
{
while(parent)
{
temp = parent;
parent = parent->next;
}
temp->next = current;
}
}
void LinkedList::Print()
{
Node* temp;
temp = root;
while(temp)
{
cout<data< temp = temp->next;
}
}
void LinkedList::Reverse()
{
Node* temp;
Node* previous;
bool first = true;
while(root)
{
temp = root->next;
if(first)
{
first = false;
root->next = NULL;
}
else
{
root->next = previous;
}
previous = root;
root = temp;
}
root = previous;
}
int main()
{
LinkedList ll ;
int choice, value;
do
{
cout<<"Insert the elements"< cin>>value;
ll.insert(value);
cout<<"Do you want to continue, Press 1 to continue"< cin>>choice;
}while(choice == 1);
ll.Reverse();
ll.Print();
cin>>choice;
}
using namespace std;
class LinkedList
{
private:
struct Node
{
int data;
Node *next;
};
Node *root;
public:
LinkedList()
{
root = NULL;
}
void insert(int);
void Print();
void Reverse();
};
void LinkedList::insert(int d)
{
Node* current = new Node;
current->data = d;
current->next = NULL;
Node* parent;
parent = root;
Node* temp;
if(root == NULL)
{
root = current;
}
else
{
while(parent)
{
temp = parent;
parent = parent->next;
}
temp->next = current;
}
}
void LinkedList::Print()
{
Node* temp;
temp = root;
while(temp)
{
cout<
}
}
void LinkedList::Reverse()
{
Node* temp;
Node* previous;
bool first = true;
while(root)
{
temp = root->next;
if(first)
{
first = false;
root->next = NULL;
}
else
{
root->next = previous;
}
previous = root;
root = temp;
}
root = previous;
}
int main()
{
LinkedList ll ;
int choice, value;
do
{
cout<<"Insert the elements"<
ll.insert(value);
cout<<"Do you want to continue, Press 1 to continue"<
}while(choice == 1);
ll.Reverse();
ll.Print();
cin>>choice;
}
Comments
Post a Comment