0

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");
}
dbush
  • 205,898
  • 23
  • 218
  • 273
Jade
  • 120
  • 1
  • 2
  • 13
  • 2
    the `main`  function in c must return an `int` your's is `void` – litelite Jul 21 '17 at 17:45
  • 1
    And you are missing an include to use `strcmp`  (You need to include string.h) – litelite Jul 21 '17 at 17:46
  • Grep all your '.h' files, and you will find strcmp(). – Martin James Jul 21 '17 at 17:48
  • Really? The compiler warning about `main` is very clear. And can very easily be found via Google. There are tons of duplicates already: https://stackoverflow.com/questions/40381960/error-main-must-return-int and https://stackoverflow.com/questions/40966790/error-main-must-return-int – Gerhardh Jul 21 '17 at 17:57
  • The colons in `::main` indicate that you are not using a C compiler. – Gerhardh Jul 21 '17 at 17:57

1 Answers1

1

Change void main() to int main(void) and add #include <string.h>

Valid signatures for main() are

  1. int main(int argc, char **argv);
  2. int main(void);

and strcmp() (as well as all str* functions) is declared in string.h).

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Kai
  • 2,529
  • 1
  • 15
  • 24