#include < iostream>
#include < stdio.h>
using namespace std;
class node
{
public: int data;
node *ptr;
};
class linkedList
{
private: node *head;
public: linkedList() //constructor
{
head = NULL;
}
void insert_front();
void reverse();
void display();
~linkedList() //destructor
{
delete head;
}
};// end of LL
void linkedList::insert_front()
{
node *newNode;
int item;
newNode = new node;
cout<< " enter the item to be inserted" << endl;
cin >> item;
newNode->data=item;
newNode->ptr=head;
head=newNode;
}
void linkedList::reverse()
{
node *current, *temp;
current=NULL;
if(head==NULL)
{
cout<< "list is empty"<< endl;
}
while(head!=NULL)
{
temp=head;
head=head->ptr;
temp->ptr=current;
current=temp;
}
head=temp;
}
void linkedList::display()
{
node *temp;
temp=head;
cout<< "\ncontents of list"<< endl;
while(temp!=NULL)
{
cout<< temp->data<< "->";
temp=temp->ptr;
}
cout<< "NULL"<< endl;
}
void main()
{
linkedList ll;
int opt;
while(opt!=0)
{
ll.insert_front();
ll.display();
cout<< "do you want to continue: yes: 1 or No: 0"<< endl;
cin>>opt;
}
ll.reverse();
cout<< "after reversal ";
ll.display();
Thursday, September 11, 2008
C++ program to Reverse a LINK LIST
Posted by Nanya at 12:20 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment