create and display a singly linked list - c

My code:
#include <stdio.h>
node * create(int);
void disp(node *,int);
typedef struct node
{
int data;
struct node *next;
};
node * create(int);
void disp(node *,int);
typedef struct node *head , *p , *c;
int i,n;
int main()
{
printf("\n Enter the number of nodes:");
scanf("%d",&n);
c=create(n);
disp(head,n);
return 0;
}
node * create(int n)
{
head = (node *)malloc(sizeof(node));
scanf("%d", &head->data);
head->next = NULL;
p=head;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=p->next;
p->next=NULL;
}
return head;
}
void disp(node *head , int n)
{
p=head;
while(p!=NULL)
{
for(i=0;i<n;i++)
{
printf("%d",p->data);
p=p->next;
}
}
}
OUTPUT:
User#Sujata:~/Desktop]$ gcc ll.c
ll.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:4: error: expected ‘)’ before ‘*’ token
ll.c:10: warning: useless storage class specifier in empty declaration
ll.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:12: error: expected ‘)’ before ‘*’ token
ll.c: In function ‘main’:
ll.c:19: error: expected identifier or ‘(’ before ‘=’ token
ll.c:20: error: expected expression before ‘head’
ll.c: At top level:
ll.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:41: error: expected ‘)’ before ‘*’ token
Got this output . Tried many times using the typedef keyword also. But does not work . Thanks in advance !

.....Your code is messy......
You need to add
typedef struct node node;
before the forward declaration of functions so that the type node is known by the compiler. Also, remove the typedef from
typedef struct node *head , *p , *c;

Related

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’

I know this question has been asked numerous times previously, however I am racking my brain trying to figure out what is causing the error as I cannot identify where the usual missing semi-colon is.
<#include<stdio.h>
#define MaxLenght 30
typedef int ElementType;
typedef int Position
typedef struct {
ElementType Elements[MaxLength];
Position Last;
} List L;
void MAKENULL_LIST(List *L){
L->Last=0;
}
Position First(List L) {
return 1;
}
Position EndList(List L) {
return L.Last+1;
}
ElementType Retrieve(Position P, List L) {
return L.Elements[P-1];
}
Position Next(Position P, List L) {
return P+1;
}
void printList(List L) {
Position P;
P = First(L);
while (P != EndList(L)) {
printf("%d ",Retrieve(P,L));
P = Next(P, L);
}
printf("\n");
}
Error thrown by compiler:
__tester__.c:26:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token } List *L;
You have several typos:
1 Replace:
#define MaxLenght 30
By
#define MaxLength 30
2 Replace:
typedef int Position
by
typedef int Position;
3 Replace:
} List L;
by
} List;
And voilà, it compiles.

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before

Yes, I know this question has been asked numerous times previously, however I am racking my brain trying to figure out what is causing the error as I cannot identify where the usual missing semi-colon is.
list.h
/* list.h */
#ifndef _LIST_H
#define _LIST_H
#define TRUE_ true
#define FALSE_ false
class LISTElement {
public:
LISTElement(void *itemPtr, int sortKey); /* initialize a list element */
LISTElement *next; /* next element on list,
NULL if this is the last */
int key; /* priority, for a sorted list */
void *item; /* pointer to item on the list */
};
class LIST {
public:
LIST(); /* initialize the list */
~LIST(); /* de-allocate the list */
bool IsEmpty(); /* is the list empty? */
void Insert(void *item, int sortKey); /* Put item into list */
void *Remove(int *keyPtr); /* Remove first item from list */
private:
LISTElement *first; /* Head of the list, NULL if list is empty */
LISTElement *last; /* Last element of list */
};
#endif /* _LIST_H */
list.c
#include <stdlib.h>
#include "list.h"
LISTElement::LISTElement(void *itemPtr, int sortKey) {
item = itemPtr;
key = sortKey;
next = NULL; /* assume we'll put it at the end of the list */
}
LIST::LIST() {
first = last = NULL;
}
LIST::~LIST() {
while (Remove(NULL) != NULL)
; // delete all the list elements
}
bool LIST::IsEmpty() {
if (first == NULL)
return TRUE_;
else
return FALSE_;
}
void LIST::Insert(void *item, int sortKey) {
LISTElement *element = new LISTElement(item, sortKey);
LISTElement *ptr;
if (IsEmpty()) { /* if list is empty, put */
first = element;
last = element;
} else if (sortKey < first->key) {
/* item goes on front of list */
element->next = first;
first = element;
} else { /* look for first element in list bigger than item */
for (ptr = first; ptr->next != NULL; ptr = ptr->next) {
if (sortKey < ptr->next->key) {
element->next = ptr->next;
ptr->next = element;
return;
}
}
last->next = element; /* item goes at end of list */
last = element;
}
}
void * LIST::Remove(int *keyPtr) {
LISTElement *element = first;
void *thing;
if (IsEmpty())
return NULL;
thing = first->item;
if (first == last) { /* list had one item, now has none */
first = NULL;
last = NULL;
} else {
first = element->next;
}
if (keyPtr != NULL)
*keyPtr = element->key;
delete element;
return thing;
}
Error thrown by compiler:
$ gcc -o list list.c
In file included from list.c:2:
list.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LISTElement’
list.h:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:50: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
This time it's not a missing semicolon, but a completely different language. The code you're trying to compile is in C++, not C. That's why you get syntax errors from a C compiler.
To fix this (and compile the code as C++), rename the file from .c to .cpp:
mv list.c list.cpp
And use g++ to compile it, not gcc:
g++ -o list list.cpp

Error on typedef struct. before '*' token

The compiler is gcc and I'm using an old version of linux
typedef struct strlist strlist;
struct strlist
{
char *data;
time_t *timestamp;
struct strlist *next;
}
strlist * list_directory(char *dirname)
{
//do something
}
The error message is:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
and it's on the line:
strlist * list_directory(char *dirname)
struct strlist
{
char *data;
time_t *timestamp;
struct strlist *next;
};
^^^
|
You are missing the ; at the end of structure declaration.
You need to end the struct declaration with a semicolon. That tripped me up so many times when I started programming in C...

Error: expected specifier-qualifier-list before

I am trying to write code to build a stack, but I'm getting compilation errors which don't make sense to me. Here is my stack.h:
struct StackNode {
void* previous;
int value;
};
struct Stack {
StackNode* top;
};
Stack* new_stack () {
StackNode stn = { NULL, 0 };
Stack* st = (Stack*) malloc(sizeof(Stack));
st->top = NULL;
return st;
}
and my main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main () {
struct Stack* st = new_stack();
return 0;
}
gcc throws these errors:
make (in directory: /home/diego/temp/stack) gcc -g -O2 -std=c99 -c
main.c In file included from main.c:4: Compilation failed. stack.h:10:
error: expected specifier-qualifier-list before ‘StackNode’
stack.h:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’
before ‘*’ token main.c: In function ‘main’: main.c:8: warning:
implicit declaration of function ‘new_stack’ main.c:8: warning:
initialization makes pointer from integer without a cast make: *
[main.o] Error 1
EDIT: I found the error. I forgot to put struct before Stack and StackNode in some lines. Always having struct on those lines solves the issue.
Change:
struct Stack {
StackNode* top;
};
to:
struct Stack {
struct StackNode* top;
};
and anywhere else StackNode or Stack is used and not preceded by struct. If you wish to not specify struct you could use a typedef.

initializing error in while loop

Here is a small program I wrote, (I am still writing it), however till this point the program on compilation should not be giving any error as per my understanding.
#include <stdio.h>
#include <stdlib.h>
struct node t1 {
int data;
struct node *next, *prev;
};
struct node *root;
root = NULL;
int main()
{
int i, j, choice, count;
printf("enter choice\n");
scanf("%d", &choice);
count = 0;
while (choice == 1) {
printf("enter a data element");
scanf("%d", &j);
count++;
}
}
void push()
{
}
void pop()
{
}
The error I get is
cc linklist.c
linklist.c:3:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
linklist.c:8:1: warning: data definition has no type or storage class [enabled by default]
linklist.c:8:1: error: conflicting types for ‘root’
linklist.c:7:14: note: previous declaration of ‘root’ was here
linklist.c:8:8: warning: initialization makes integer from pointer without a cast [enabled by default]
I use gcc and Ubuntu 11.04.
What is the reason that upon compiling the code I get above warning.
struct node *root;
root = NULL;
You can't assign like that outside a function. Drop the root = NULL since it's implicit for objects with static storage (such as global variables).
EDIT
As spotted by Tom Dignan the struct declaration is also wrong:
struct node t1 { ... };
^^
You can't put a statement like root = NULL; at top-level (outside of any function). Do
struct node *root = NULL;
(The = NULL part is actually optional; a global or static pointer is automatically null.)
For one, you have an assignment statement outside of main or a function.
root = NULL;
I have not tried anything else.
struct node t1 {
int data;
struct node *next, *prev;
};
You want to create alias for struct node. It shoud be:
typedef struct node { /* typedef! */
int data;
struct node *next, *prev;
}t1; /* alternative name go here */

Resources