Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   implementation of doubly linked list using list.h (http://www.go4expert.com/forums/implementation-doubly-linked-list-using-t28526/)

arjunaugustine 5Jun2012 18:47

implementation of doubly linked list using list.h
 
I have created a basic linkedlist program, and debugged it, but one single error repeated thrice in three different function calls of the same function simply seems not to go away.
the error is in line containing list_entry fuction and it says:

gcc linkedlist.c
linkedlist.c: In function ‘create_node’:
linkedlist.c:46: error: expected expression before ‘struct’
linkedlist.c: In function ‘delete_node’:
linkedlist.c:70: error: expected expression before ‘typeof’
linkedlist.c: In function ‘print_list’:
linkedlist.c:90: error: expected expression before ‘struct’


the file and the header has been attatched, the code has also been copy-pasted below. Please correct my mistake.

Your valuable help is acknowledged..


Code:

#include <stdio.h>
#include </root/Downloads/arjun/list.h>

int roll, a; char sex;

/*
struct list_head
{
  struct list_head *next, *prev;     
};
*/

struct roll_struct
{
  struct list_head list;
  int rno;
  char mf;
};

struct roll_struct roll_list =
  {
    .mf = 'n',
    .rno = 0,
    .list = LIST_HEAD_INIT(roll_list.list)
  };

struct list_head *ptr, *next;
struct roll_struct *element = NULL;


void create_node()
{
  enter_node:        //label
  printf("Enter roll no and sex: /n");
  scanf("%d%c",roll,sex);
  struct roll_struct new_student =
  {
    .mf = sex,
    .rno = roll,
    .list = LIST_HEAD_INIT(new_student.list)
  };
  printf("Node created, Preparing to add node:/n");
 
  list_for_each(ptr,&roll_list.list)
  {
    element = list_entry (ptr, struct roll_struct, list);
    if( element->rno ==new_student.rno)
    {
        printf("ERROR: Roll no already exists! Please re-enter node details:/n");
        goto enter_node;
    }
    else if( element->rno > new_student.rno)
    {
        list_add_tail(&new_student.list, ptr);
        printf("Node added to list in ascending order of roll no./n");
        return;
    }
  }
  list_add(&new_student.list, ptr);
  printf("Node added to end of list./n");
}
   
void delete_node()
{
  del_node:
  printf("Enter the roll number to delete:/t");
  scanf("%d",roll);
  list_for_each_safe(ptr, next, &roll_list.list)
  {
    element = list_entry(ptr,typeof(roll_list),list);        //list_entry(pos->member.next, typeof(*pos), member))
    if( element->rno == roll)
    {
        list_del(ptr);
        printf("Roll no found and deleted!/n");
        return;
    }
  }
  printf("Roll no not found. Do you want to re-enter rno?/n");
  printf("Press 1 if Yes, or any other key to continue.../n");
  scanf("%d",a);
  if( a == 1 )
  goto del_node;
}

void print_list()
{
  printf("The Roll number and sex details in the roll-list are printed below:/n");
  list_for_each(ptr,&roll_list.list)
  {
    element = list_entry(ptr->next, struct roll_struct, list);    //list_entry(blist.list.next,struct a_list,list);
    printf("/nRoll No: %d/t Sex: %c",element->rno,element->mf);
  }



int main()
{
  printf("hello there :) /n ");
 
  while(1)
    {
      printf("/nwhat would u like to do?/n");
      printf("type 1 to create and add a node,/ntype 2 to delete node,/ntype 3 to print list,/ntype 4 to exit;/n");
      scanf("%d",a);
     
      switch(a)
    {
    case '1' : create_node(); break;
    case '2' : delete_node(); break;
    case '3' : print_list();  break;
    case '4' : return 0;
    default: printf("Invalid option, please try again.");
    }
    }

return 0;
}


xpi0t0s 6Jun2012 11:46

Re: implementation of doubly linked list using list.h
 
Assuming line 46 is this one:
Code:

element = list_entry (ptr, struct roll_struct, list);
you can't put a typename (struct roll_struct) there. You must use the name of a variable. You have the same error on the other two lines.


All times are GMT +5.5. The time now is 09:27.