Some help with a Linked lIst
Okay I have updated my code quite a bit. I am getting a new problem, but it seems to be on a correct path. Now when I enter in the numbers it just continually spits out the first number I entered instead of moving to the next number.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#inclu开发者_运维问答de <cType.h>
#include "list.h"
#define amount 3
//Prototypes
void printList(LIST* number);
int main(){
int i;
int* dataPtr;
int number;
LIST* numberList;
numberList = createList();
printf("Please enter %d numbers for the linked list\n", amount);
for(i = 0; i < amount; i++){
printf("#%d: ", i+1);
scanf("%d", &number);
dataPtr = malloc(sizeof(int));
*dataPtr = number;
addNode(numberList, dataPtr);
}
printList(numberList);
system("PAUSE");
return 0;
}
void printList(LIST* number){
int* dataPtr;
while (!emptyList(number)){
traverse(number,0, (void*)&dataPtr);
printf("%d\n", *dataPtr);
addNode(number, dataPtr);
}
}
list.h
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cType.h>
//List ADT Type Definitions
typedef struct node{
void* dataPtr;
struct node* link;
} NODE;
typedef struct{
int count;
NODE* pos;
NODE* head;
NODE* rear;
} LIST;
//Prototype Declarations
LIST* ceateList(void);
bool traverse (LIST* pList, int fromWhere, void** dataOutPtr);
int listCount (LIST* pList);
bool emptyList (LIST* pList);
bool fullList (LIST* pList);
bool addNode (LIST* pList, void* dataInPtr);
list.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cType.h>
#include "list.h"
LIST* createList(void){
LIST* list;
list = (LIST*) malloc (sizeof(list));
if(list){
list->head = NULL;
list->rear = NULL;
list->count = 0;
}
return list;
}
bool addNode(LIST* pList, void* dataInPtr){
NODE* pNew;
if(!(pNew = (NODE*) malloc(sizeof(NODE))))
return false;
pNew->dataPtr = dataInPtr;
pNew->link = NULL;
if(pList->count == 0){
pNew->link = pList->head;
pList->head = pNew;
if(pList->count == 0)
pList->rear = pNew;
}
else{
pNew->link = pNew;
if(pNew->link == NULL)
pList->rear = pNew;
}
(pList->count)++;
return true;
}
bool emptyList(LIST* pList){
return(pList->count == 0);
}
bool fullList(LIST* pList){
NODE* temp;
if((temp = (NODE*)malloc(sizeof(*(pList->head))))){
free(temp);
return false;
}
return true;
}
int listCount(LIST* pList){
return pList->count;
}
bool traverse(LIST* pList, int fromWhere, void** dataPtrOut){
if(pList->count == 0)
return false;
if(fromWhere == 0){
pList->pos = pList->head;
*dataPtrOut = pList->pos->dataPtr;
return true;
}
else{
if (pList->pos->link == NULL)
return false;
else{
pList->pos = pList->pos->link;
*dataPtrOut = pList->pos->dataPtr;
return true;
}
}
}
list = (LIST*) malloc (sizeof(list));
You have the size of a pointer, not that of a struct.
When you call _insert()
you havn't initialized pPre
. In C, uninitialized memory is not nessescarily NULL
.
Also, if I were you I'd remove the pos
pointer from the LIST
type - using it makes your code very much not reentrant.
I think learning to use a debugger would be a good idea. It is an essential piece of any devs skill set.
If this is on a Linux system using gcc (most likely) then compile the code with -g and run it using gdb. If you need more guidance on this then says so
PS: I agree with earlier poster that you should not use all caps variable names. Traditionally all caps are for macros and constants
精彩评论