I have to generate a reversed linked list and these are my prerequisites
Define a structure:
struct node
{
int data;
struct node * link;
}
Include functions
append --- to add data at the end of the linked list.
Reverse --- to reverse the linked list.
display --- to display all the data in the linked list.
void append ( struct node **, int ) ;
void display ( struct node * ) ;
void reverse (struct node **);
Error I receive:
1.‘::main’ must return ‘int’
void main()
2.‘strcmp’ was not declared in this scope
z = strcmp(ch,ch1);
My code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void append(struct node **head);
void reverse(struct node **head);
void display(struct node *p);
void main()
{
struct node *p = NULL;
append(&p);
printf("The elements in the linked list are: ");
display(p);
printf("The elements in the reversed linked list are: ");
reverse(&p);
display(p);
}
void reverse(struct node **head)
{
struct node *p,*q,*r;
p=q=r=*head;
p = p->link->link;
q = q->link;
r->link = NULL;
q->link = r;
while(p != NULL)
{
r = q;
q = p;
p = p->link;
q->link = r;
}
*head = q;
}
void append(struct node **head)
{
int c,z;
char ch[10] = "Yes";
char ch1[10];
struct node *temp,*rear;
do
{
printf("Enter the value:\n");
scanf("%d",&c);
temp = (struct node*)malloc(sizeof(struct node));
temp->data=c;
temp->link=NULL;
if(*head == NULL)
{
*head = temp;
}
else
{
rear->link = temp;
}
rear = temp;
printf("\nDo you want to add another node? Type Yes/No\n");
scanf("%s",ch1);
z = strcmp(ch,ch1);
}
while(z == 0);
printf("\n");
}
void display(struct node *p)
{
while(p != NULL)
{
printf("%d ",p->data);
p=p->link;
}
printf("\n");
}