Double linked lists and a structure to hold them plus a name.

This is a discussion on Double linked lists and a structure to hold them plus a name. within the C Programming forums, part of the General Programming Boards category; Assignment: Create a double linked list and then create a structure to hold each double linked list with a name. ...

  1. #1
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42

    Double linked lists and a structure to hold them plus a name.

    Assignment: Create a double linked list and then create a structure to hold each double linked list with a name. I figured that with each double linked list that the user can create I can just add it to the end of a single linked list.

    The link structure below is used to hold the name of the list, the list created by the user, and a pointer to the next link. To make a list the user just enters: "c listName" from stdin. I can create mutliple lists and it seems just fine until I use void printList(..) to see if the single linked list is indeed intact. If I would enter: c List1 then c List2 and then c List3 and print them out to see if I have List1 List2 List3, I get List3 List3 List3.

    I've tried for hours (pathetically) to see what can be wrong and I just cannot get it. A hint to the next step would greatly help me out!


    Code:
    //Structure for the collection of double-linked lists.
    struct link {
    	char *name;
    	struct link *next;
    	list currentList;
    };
    
    
    void insertNewList(struct link **head, char *data) {
    	struct link* temp;
    	struct link* temp1;
    	list x;
    
    	x = create();
    	temp = (struct link*) malloc(sizeof(struct link));
    
    	temp -> name = data;
    	temp -> currentList = x;
    	temp -> next = *head;
    
    	*head = temp;
    	printf("The name of the new list is: %s\n", (*head) -> name);
    }
    
    void insertStringToList(struct link **head, char *data, char *listName) {
    	struct link* temp;
    	temp = *head;
    
    	if (strcmp(temp -> name, listName) == 0) {
    		printf("we found a match.\n");
    	} else {
    		printf("the listName was not found.\n");
    	}
    }
    
    void printList(struct link **head) {
    	struct link* temp;
    	temp = *head;
    
    	printf("%s  ", temp -> name);
    	while (temp != NULL) {
    		printf("%s  ", temp -> name);
    		temp = temp -> next;
    	}
    	printf("\n");
    }
    Last edited by Nit; 03-21-2002 at 12:16 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void insertNewList(struct link **head, char *data) {
    >	struct link* temp;
    >	struct link* temp1;
    >	list x;
    
    >	x = create();
    >	temp = (struct link*) malloc(sizeof(struct link));
    
    
    >	temp -> name = data;
    //Should be:
    	temp -> name = malloc(sizeof(strlen(data)+1));
    	strcpy(temp -> name,  data);

Popular pages Recent additions subscribe to a feed

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