Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 7th, 2007, 04:55 AM
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
  #2 (permalink)  
Old December 1st, 2007, 05:16 AM
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
2D Linked List sjf905 C# 0 October 8th, 2006 11:02 PM
C# Object Linked List millsbruce C# 5 July 12th, 2005 04:15 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.