-
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;
}
}
}
-
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.
-
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;
}
}
}
-
Code:
for( ; currentPtr; currentPtr = currentPtr->next )
printf("%c", currentPtr->data );
Use %c when printing a single character.
Quzah.