Home > C Examples > PROGRAM TO TRAVERSE LINKED LIST IN REVERSE ORDER

PROGRAM TO TRAVERSE LINKED LIST IN REVERSE ORDER

#include<iostream.h>        //HEADER FILES
#include<ctype.h>
#include<stdio.h>
#include<process.h>

struct node                 //STRUCTUREFOR LIST
{
char data;
node *next;
};

node *head=NULL;
node *current=NULL;
node *temp,*previous;

void print(node *, node *);          //FUNCTION TO PRINT LIST

void main()                          //MAIN() STARTS HERE
{
char cho;
do
{
temp=new(node);
cout<<"\n ENTER THE CHARACTER :";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
current=temp;
}
else
{
current->next=temp;
current=temp;
}
cout<<"\n WANT TO ADD MORE? (Y/N)";
cin>>cho;
}while(tolower(cho)=='y');
cout<<"\n LIST IN THE REVERSE ORDER IS \n";
temp=head;
previous=NULL;
print(temp,previous);
}

void print(node *temp,node *previous)     //FUNCTION DEFINITION
{
while(1)
{
if(temp->next==NULL)
{
printf("%c%s",(temp->data),"  ");
if(previous!=NULL)
previous->next=NULL;
if(temp==head)
exit(0);
delete(temp);
temp=head;
}
else
print(temp->next,temp);
}
}

About these ads
Categories: C Examples Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Journey to SQL Authority with Pinal Dave

SQL, SQL Server, MySQL, Big Data and NoSQL

Bug: An elusive creature living in a program that makes it incorrect. The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed.

CodeProject Latest Articles

Bug: An elusive creature living in a program that makes it incorrect. The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed.

Follow

Get every new post delivered to your Inbox.

Join 31 other followers

%d bloggers like this: