Write
a program to reverse the given string
using Single linked list.
#include <stdio.h>
#include <string.h>
struct node
{
char data;
struct node *next;
};
struct node *head=NULL,*c,*p,*r;
void create(char value)
{
struct node * new = (struct node *)malloc(sizeof(struct node));
new->data = value;
new->next=NULL;
if(head == NULL)
head = new;
else
{
c=head;
while(c->next != NULL)
{
c=c->next;
}
c->next=new;
}
}
display()
{
if(head == NULL)
printf("list is empty");
else
{
c=head;
while(c->next!=NULL)
{
printf("\n %c\t",c->data);
c=c->next;
}
printf(" %c\n",c->data);
}
}
main()
{
char s1[50];
int i;
printf("enter a string");
scanf("%s",s1);
for(i=0;s1[i]!='\0';i++)
create(s1[i]);
c=head;p=NULL;
while(c!=NULL)
{
r=p;
p=c;
c=c->next;
p->next=r;
}
head=p;
display();
}
No comments:
Post a Comment