View Single Post
  #2 (permalink)  
Old December 1st, 2007, 05:16 AM
C@uark C@uark is offline
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

some suggestions:
no need to have gobal variables head, tail, traversal, and you really dont want your list operations doing io routines. this should be left to the calling routine(main in this case).

Have two files Linked_List.h and Linked_List.c

in your Linked_List.h file declare what is commonly referred to as a list header(or container), and all operations that you wish to perform on your list. My example has operations newList, newNode, insertHead, insertTail, insertAscending declared the rest is up to you.

#ifndef Link_List
#define Link_List

typedef struct node_object // container for data
{
int key; //in case you want ordered insertion such as ascending order
int value;
struct node_object *next;
}node;

typedef struct list_object // container for your list
{
node *head;
node *tail;
}list;

// operation declarations (they are define in the Linked_List.c)
// extern key word gives them visiblity outside file scope
extern node* newNode(int k, int v); //creates a new node for insertion
extern list* newList(node *aNode); //creates a new list
extern list* insertHead(list *theList, node *insertNode); //inserts at the beginning of the list
extern list* insertTail(list *theList, node *insertNode); //inserts at the end of a list
extern list* insertAscending(list *theList, node* insertNode); //searches the list for a specific insertion point base on the key of the insertNode and insert the node in ascending

//declare any other operartion that you may need

#endif


the Linked_List.c file for the linked list defines your operations on your list

#include <any files needed>
#include "Linked_List.h" // like this if header is in same file as source code, else full path and file name

node* newNode(int k = 0, int v = 0 ) //default initializer if none supplied
{
node *newNode;
newNode = (node*) malloc(sizeof(node));
if(newNode) //if valid node address initialize
{
     newNode->key = k;
     newNode->value = v;
     newNode->next = NULL;
}
return newNode;
}

list* newList(node *aNode = NULL) //default initializer is none supplied
{
list *newList;
newList = (list*) malloc(sizeof(list));
if(newList) //if valid list address initialize
{
    newList->head = aNode;
    newList->tail = aNode;
}
return newList;
}

list* insertHead(list *theList, node *insertNode);
{
if(theList->head == NULL) //empty list
{
     theList->head = insertNode; //first node inserted
     theList->tail = insertNode; //only one node in list then it is also the tail
}
else // not empty list requires so rearranging of pointers
{
     insertNode->next = theList->head; // do this first so you dont lose the rest of the list when you insert new node
     theList->head = insertNode; //newNode insert w/ rest of list attached to it
     // since we inserted at the head the tail is still the tail
    }
    return theList; // return the updated list
}

list* insertTail(list *theList, node *insertNode)
{
if(theList->tail == NULL) //empty list
{
     list->tail = insertNode;
     list->head = insertNode; //only node inserted must be head to
}
else // not empty list
{
     list->tail->next = insertNode; //tail is the end of the list so just attach to the last node
     list->tail = insertNode; //insertNode is now the new tail
     // insert at the end of the list so the head is still the same
}
return theList; //return updated list
}

list* insertAscending(list* theList, node *insertNode, int key)
{
// I am leaving this and any other operations up to you

return theList; // dont forget to return the updated list;
}

Now that you have your Linked_List.h and Link_List.c files you are ready to them in your main program.

#include <stdio.h>
#include <stdlib.h>
#include "Linked_List.h"


/*
1. Insert Behind
2. Insert In Front
3. Random Insert -nth
4. Remove Behind until zero or one only
5. Remove In Front
6. Random Remove -nth
7. Display
8. Quit //you might a quit option
*/

int main(int argc, char** argv)
{
    int choice;
    int value;
    list *theList = NULL;
    node *new_node = NULL;

    theList = newList(); // only want one list so create outside of do while
    do
    {
     //print menu here
     //get user choice here

     switch(choice)
     {
     case 1:
     {
         printf("Enter a value to be inserted: ")
         fflush(stdin); //flush any thing left in stdin buffer
         scanf("%d",&value); //get new_node value
         printf("\n"); //new line after user input echos
         new_node = newNode(value,value); // want a new_node everytime, key is same as value here
         theList = insertTail(theList, new_node); //insert and end of list
     }break;
     case 2:
     {
            //more stuff here
     }break;
     .
     .
     .
     default
            printf("Invalid menu option\n");
     }// end switch
}while(choice != 8);
}//end main
Reply With Quote