View Single Post
  #1 (permalink)  
Old November 7th, 2007, 04:55 AM
Peter_APIIT Peter_APIIT is offline
Authorized User
 
Join Date: May 2007
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Peter_APIIT
Default Linked LIst Progam in C

Hello respective expert programmer,

i truly a noob in programming.

I have create a program that use linked list data structures but haven't successful.

Basically, my idea is like this, let the user choose from the menu and insert linked what ever they like until they have satisfied.After that, choose again from the menu to display the list or to something else but i think my list has scope problem where in display the list become NULL again.

My malloc memory allocation is done at insert function and display function is in another function. Therefore, the list become NULL in display.

How to solve this problem ? I try to use global variable in some cases.

Please help me to solve this problem.


Below is my program:
#include<stdio.h>
#include<stdlib.h>
#include "Link_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
*/

// Global variable for head and tail
  struct node *head, *tail, *traversal;
  struct node *list;

// ----------------------------------------------------
void InsertBehind(struct node *);


void Display(struct node *);

// -----------------------------------------------------
int main(int argc, char *argv[])
{
    int choice;

    head = malloc(sizeof(struct node));
    tail = malloc(sizeof(struct node));
    traversal = malloc(sizeof(struct node));

    head = list;

    do
    {
        system("cls");
        printf("\n\n\n\n\t\t\t\t Main Menu\n\n\t\t\t\t1. Insert Behind\n\t\t\t\t2. Insert In Front\n\t\t\t\t3. Random Insert\n");
        printf("\t\t\t\t4. Remove Behind\n\t\t\t\t5. Remove In Front\n\t\t\t\t6. Random Remove\n\t\t\t\t7. Display\n");
        printf("\n\n\t\t\tPlease Enter a valid choice from 1 to 7");
        printf("\n\n\t\t\t\t\tChoice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            InsertBehind(list);
            break;
        case 7:
            Display(list);
            break;
        default:
            printf("Invalid Choice");
            break;
        }
    }while(1);


    // To avoid memory leak
    free(head);
    free(tail);
    free(traversal);
    free(list);


    return 0;
}
// ------------------------------------------------------
void InsertBehind(struct node *list)
{
    list = malloc(sizeof(struct node));

    if (list != NULL)
    {
        system("cls");
        printf("Enter a value into the Linked List : ");
        scanf("%d", &list->value);
// printf("%d", list->value);

        // If head not pointed to first node
        // then head pointed to first node
        // else tail pointed to second node
        if (head != list)
        {
            head = list;
        }
        else
        {
            list->next = list;
            tail = list->next;

            if (tail->value == 500)
            {
                traversal = head;

                while(traversal != NULL)
                {
                    printf("The value of List is ", traversal->value);
                    traversal = list->next;
                }
            }
        }
    }
    else
    {
        printf("Heap Memory not enough");
    }
}
// ------------------------------------------------------
void Display(struct node *list)
{
    traversal = head;

    while(traversal != NULL)
    {
        printf("The value of List is ", traversal->value);
        traversal = list->next;
    }
}

Header File:

#ifndef Link_List
#define Link_List

struct node
{
    int value;
    struct node *next;
};

#endif

Thanks for your help.

Your help is greatly appreciated by me and others.

Linux is the best OS in the world.
__________________
Linux is the best OS in the world.
Reply With Quote