linked list

This is a discussion on linked list within the C Programming forums, part of the General Programming Boards category; I can't seem to get my linked list to execute. I am using miracle c compiler and i found a ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    linked list

    I can't seem to get my linked list to execute. I am using miracle c compiler and i found a tutorial online here. When I run it I get the following error:

    C:\Program Files\Miracle C\linkedlistdemo.c: line 12: Parse Error, expecting `'{''
    'typedef struct { int dataitem'
    aborting compile

    I checked and my brackets are there. Here is a copy of the code. I need to implement a linked list for FIFO page algorithm, but this linked list won't even work to get me started.

    /* */
    /* queue.c */
    /* Demo of dynamic data structures in C */

    #include <stdio.h>

    #define FALSE 0
    #define NULL 0

    typedef struct {
    int dataitem;
    struct listelement *link;
    } listelement;

    void Menu (int *choice);
    listelement * AddItem (listelement * listpointer, int data);
    listelement * RemoveItem (listelement * listpointer);
    void PrintQueue (listelement * listpointer);
    void ClearQueue (listelement * listpointer);

    main () {
    listelement listmember, *listpointer;
    int data,
    choice;

    listpointer = NULL;
    do {
    Menu (&choice);
    switch (choice) {
    case 1:
    printf ("Enter data item value to add ");
    scanf ("%d", &data);
    listpointer = AddItem (listpointer, data);
    break;
    case 2:
    if (listpointer == NULL)
    printf ("Queue empty!\n");
    else
    listpointer = RemoveItem (listpointer);
    break;
    case 3:
    PrintQueue (listpointer);
    break;

    case 4:
    break;

    default:
    printf ("Invalid menu choice - try again\n");
    break;
    }
    } while (choice != 4);
    ClearQueue (listpointer);
    } /* main */

    void Menu (int *choice) {

    char local;

    printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\
    \t3 to print queue\n\t4 to quit\n");
    do {
    local = getchar ();
    if ((isdigit (local) == FALSE) && (local != '\n')) {
    printf ("\nyou must enter an integer.\n");
    printf ("Enter 1 to add, 2 to remove, 3 to print, 4 to quit\n");
    }
    } while (isdigit ((unsigned char) local) == FALSE);
    *choice = (int) local - '0';
    }

    listelement * AddItem (listelement * listpointer, int data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
    while (listpointer -> link != NULL)
    listpointer = listpointer -> link;
    listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
    listpointer = listpointer -> link;
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return lp;
    }
    else {
    listpointer = (struct listelement *) malloc (sizeof (listelement));
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return listpointer;
    }
    }

    listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
    }

    void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
    printf ("queue is empty!\n");
    else
    while (listpointer != NULL) {
    printf ("%d\t", listpointer -> dataitem);
    listpointer = listpointer -> link;
    }
    printf ("\n");
    }

    void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
    listpointer = RemoveItem (listpointer);
    }
    }

  2. #2
    Sayeh
    Guest
    Your working with too much code. Try creating a small set of routines that manage a linked list. work on the concept itself before you try to utilize it.

    Code:
    typdef struct linkS
       {
       struct linkS *nextLink;
       struct linkS *prevLink;
       ...
       data
       ...
       }link;
    
    link *root;
    
    int main(void);
    addlink(link*);
    killlink(link*);

    very straightforward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21