British0zzy
August 15th, 2008, 11:10 PM
I'm trying to write my first program to use linked lists and I'm having pointer troubles. I want to use the list as a stack (first in, last out).
When I run the program, the 'head' of the list seems to point NULL. I'm not sure why this is. Here is my test code to get them up and running.
#import <stdio.h>
#import <stdlib.h>
typedef struct node {
int data;
struct node* next;
} LList;
void push(LList* head, int n);
int pop(LList* head);
int main (int argc, char *argv[])
{
LList* stack1 = NULL;
LList* stack2 = NULL;
int a[] = {1, 2, 3, 4};
size_t i;
for (i = 0; i < 4; ++i) {
push(stack1, a[i]);
}
for (i = 0; i < 4; ++i) {
push(stack2, a[3 - i]);
}
printf("Stack1:\n");
for (i = 0; i < 4; ++i) {
printf("%i\n", pop(stack1));
}
printf("Stack2:\n");
for (i = 0; i < 4; ++i) {
printf("%i\n", pop(stack2));
}
return 0;
}
void push(LList* head, int n)
{
if (head == NULL) {
LList* newNode = malloc(sizeof(LList));
if((head = newNode) == NULL) {
printf("Error: Memory allocation failed.\n");
return;
}
newNode->next = NULL;
newNode->data = n;
}
else {
LList* newNode = malloc(sizeof(LList));
if(newNode == NULL) {
printf("Error: Memory allocation failed.\n");
return;
}
newNode->next = head;
head = newNode;
newNode->data = n;
}
return;
}
int pop(LList* head)
{
if (head != NULL) {
LList* temp;
int n;
n = head->data;
temp = head->next;
free(head);
head = temp;
return n;
}
else {
printf("Error: Stack is empty.\n");
return 0;
}
}
When I run the program, I receive the 'Error: Stack is empty' message for each call of pop.
I'm pretty sure the problem is the function push. I've looked over it a bunch of times and can't tell where I made a mistake. I'm a beginner so its probably some obscure rule I forgot.
When I run the program, the 'head' of the list seems to point NULL. I'm not sure why this is. Here is my test code to get them up and running.
#import <stdio.h>
#import <stdlib.h>
typedef struct node {
int data;
struct node* next;
} LList;
void push(LList* head, int n);
int pop(LList* head);
int main (int argc, char *argv[])
{
LList* stack1 = NULL;
LList* stack2 = NULL;
int a[] = {1, 2, 3, 4};
size_t i;
for (i = 0; i < 4; ++i) {
push(stack1, a[i]);
}
for (i = 0; i < 4; ++i) {
push(stack2, a[3 - i]);
}
printf("Stack1:\n");
for (i = 0; i < 4; ++i) {
printf("%i\n", pop(stack1));
}
printf("Stack2:\n");
for (i = 0; i < 4; ++i) {
printf("%i\n", pop(stack2));
}
return 0;
}
void push(LList* head, int n)
{
if (head == NULL) {
LList* newNode = malloc(sizeof(LList));
if((head = newNode) == NULL) {
printf("Error: Memory allocation failed.\n");
return;
}
newNode->next = NULL;
newNode->data = n;
}
else {
LList* newNode = malloc(sizeof(LList));
if(newNode == NULL) {
printf("Error: Memory allocation failed.\n");
return;
}
newNode->next = head;
head = newNode;
newNode->data = n;
}
return;
}
int pop(LList* head)
{
if (head != NULL) {
LList* temp;
int n;
n = head->data;
temp = head->next;
free(head);
head = temp;
return n;
}
else {
printf("Error: Stack is empty.\n");
return 0;
}
}
When I run the program, I receive the 'Error: Stack is empty' message for each call of pop.
I'm pretty sure the problem is the function push. I've looked over it a bunch of times and can't tell where I made a mistake. I'm a beginner so its probably some obscure rule I forgot.