simple Linked Lists help

This is a discussion on simple Linked Lists help within the C Programming forums, part of the General Programming Boards category; hey everyone, im just trying to add a new node to the end of this list and print the list ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    simple Linked Lists help

    hey everyone,

    im just trying to add a new node to the end of this list and print the list out...can someone tell me what i am doing wrong..thanks alot

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct listNode { 
    	char data;
    	struct listNode *nextPtr;
    };
    
    typedef struct listNode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;
    
    /*prototypes*/
    void insert(LISTNODEPTR *, char);
    void printList(LISTNODEPTR); 
    
    int main(void) {
    	
    	LISTNODEPTR startPtr = NULL;
    	char nodedata;
    	
    	printf("\nEnter the letter you would like to add to the list\n");
    	
    	while(scanf("%d",&nodedata)) {
    		printf("\nSorry thats not a letter or string\n");
    	}
    
    	insert(&startPtr, nodedata);
    	printList(startPtr);
    
    }
    
    void insert(LISTNODEPTR *sPtr, char value) {
    
    	LISTNODEPTR newPtr;
    	LISTNODEPTR previousPtr;
    	LISTNODEPTR currentPtr;
    
    	if((newPtr = malloc(sizeof(LISTNODE))) == NULL) {
    
    		printf("NOT ENOUGH MEMORY TO CREATE A NEW NODE OF %d SIZE", sizeof(LISTNODE));
    		exit(1);
    
    	}
    
    	newPtr->data = value;
    	newPtr->nextPtr = NULL;
    
    	previousPtr = NULL;
    	currentPtr = *sPtr;
    
    	while(currentPtr != NULL) {
    
    		previousPtr = currentPtr;
    		currentPtr = currentPtr->nextPtr;
    
    	}
    
    	if(previousPtr == NULL) {
    
    		*sPtr->nextPtr = newPtr; 
    		
    	}
    
    	else {
    		
    		previousPtr->nextPtr = newPtr;		
    
    	}
    
    }
    
    void printList(LISTNODEPTR currentPtr) {
    
    	if(currentPtr == NULL) {
    
    		printf("List is empty");
    		exit(1);
    
    	}
    
    	else{
    		
    		printf("\nList Contains:");
    
    		while(currentPtr != NULL) {
    
    			printf("%s -->", currentPtr->data);
    			currentPtr = currentPtr->nextPtr;
    
    		}
    }
    
    
    
    
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    currentPtr = *sPtr;
    
    	while(currentPtr != NULL) {
    
    		previousPtr = currentPtr;
    		currentPtr = currentPtr->nextPtr;
    
    	}
    The above would be much cleaner written:
    Code:
    for( currentPtr = *sPtr; currentPtr != NULL; currentPtr = currentPtr->next )
        previousPtr = currentPtr;
    Your real problem is that you never set up your inital node correctly:
    Code:
    if(previousPtr == NULL) {
    		*sPtr->nextPtr = newPtr; 
    }
    It should be:
    Code:
    if(previousPtr == NULL) {
    		*sPtr = newPtr; 
    }
    In the future, please explain what your program is doing, what errors you're getting, if any, etc, so we don't have to read your entire program to have an idea where to start looking. It saves us both time that way.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    linked list bug

    hi all,

    i still couldnt fix the bug...so ive just tried to copy an example out of a textbook.. the code below simply puts the letter a in a list ...and then i attempt to print the list...when i try to print the list is shows no output....can someone please help..thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct listNode { 
    	char data;
    	struct listNode *nextPtr;
    };
    
    typedef struct listNode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;
    
    /*prototypes*/
    void insert(LISTNODEPTR *, char);
    void printList(LISTNODEPTR); 
    
    int main(void) {
    	
    	LISTNODEPTR startPtr = NULL;
    	char nodedata = 'a';
    	
    	insert(&startPtr, nodedata);
    	printList(startPtr);
    	
    	return 0;
    
    }
    
    void insert(LISTNODEPTR *sPtr, char value) {
    
    	LISTNODEPTR newPtr;
    	LISTNODEPTR previousPtr;
    	LISTNODEPTR currentPtr;
    
    	if((newPtr = malloc(sizeof(LISTNODE))) == NULL) {
    
    		printf("NOT ENOUGH MEMORY TO CREATE A NEW NODE OF %d SIZE", sizeof(LISTNODE));
    		exit(1);
    
    	}
    
    	newPtr->data = value;
    	newPtr->nextPtr = NULL;
    
    	previousPtr = NULL;
    	currentPtr = *sPtr;
    
    	while(currentPtr != NULL) {
    
    		previousPtr = currentPtr;
    		currentPtr = currentPtr->nextPtr;
    
    	}
    
    	if(previousPtr == NULL) {
    
    		newPtr->nextPtr = *sPtr;
    		*sPtr = newPtr;
    	}
    	
    	else {
    		
    		previousPtr->nextPtr = newPtr;
    		newPtr->nextPtr = currentPtr;
    
    	}
    
    }
    
    void printList(LISTNODEPTR currentPtr) {
    
    	if(currentPtr == NULL) {
    
    		printf("List is empty");
    		exit(1);
    
    	}
    
    	else{
    		
    		printf("\nList Contains:");
    
    		while(currentPtr->nextPtr != NULL) {
    
    			printf("%s -->", currentPtr->data);
    			currentPtr = currentPtr->nextPtr;
    
    		}
                     }
    
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( ; currentPtr; currentPtr = currentPtr->next )
        printf("%c", currentPtr->data );
    Use %c when printing a single character.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  3. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. Linked Lists -- Again!!! Help!!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2002, 07:27 PM

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