#include <stdio.h>
struct Node
{
struct Node* next;
int data;
};
struct List
{
struct Node* head;
int size;
};
struct Node initNode(struct Node* nextNode, int x)
{
struct Node newNode;
newNode.next = nextNode;
newNode.data = x;
return newNode;
}
struct List initList()
{
struct List newList;
newList.head = NULL;
newList.size = 0;
return newList;
}
void insert(struct List* list, int x)
{
struct Node newNode = initNode(list->head, x);
list->head = &newNode;
list->size++;
}
void printList(struct List* list)
{
int k = 0;
struct Node *current = (*list).head;
while(current != NULL)
{
k++;
printf("%d\n", current->data);
current = (*current).next;
if(k>50)
{
break;
}
}
printf("The size of the list is %d",list->size);
}
int main()
{
struct List myList = initList();
insert(&myList, 5);
printList(&myList);
printf("The size is %d\n", myList.size);
int i;
for( i = 0; i < 10; i++)
{
insert(&myList, i);
}
printf("The size is %d\n", myList.size);
printf("%d\n", (myList.head)->data);
printf("%d\n", myList.head);
printList(&myList);
}
So here's my code. I am trying to implement a linked list. The main problem I have is with the printList. It only prints memory locations (as well as the other printf's in main that I added as tests). I've tried (*current).data which still only prints memory locations and I've tried *(current->data) which returns an error when I compile. After inserting the five, the printlist runs once, printing one memory location as expected, but then when I insert 10 more values in the for loop, then print again, I get an infinite loop and it ends up printing the same address infinitely. I tried adding that k so the printList will print a maximum of 50 times to prevent the infinite loop and that compiles fine, but when I run, the program prints one memory location (presumably the first printList) and then I get a Bus Error.
I am already a java programmer, but C is really giving me a hell of a time.
struct Node
{
struct Node* next;
int data;
};
struct List
{
struct Node* head;
int size;
};
struct Node initNode(struct Node* nextNode, int x)
{
struct Node newNode;
newNode.next = nextNode;
newNode.data = x;
return newNode;
}
struct List initList()
{
struct List newList;
newList.head = NULL;
newList.size = 0;
return newList;
}
void insert(struct List* list, int x)
{
struct Node newNode = initNode(list->head, x);
list->head = &newNode;
list->size++;
}
void printList(struct List* list)
{
int k = 0;
struct Node *current = (*list).head;
while(current != NULL)
{
k++;
printf("%d\n", current->data);
current = (*current).next;
if(k>50)
{
break;
}
}
printf("The size of the list is %d",list->size);
}
int main()
{
struct List myList = initList();
insert(&myList, 5);
printList(&myList);
printf("The size is %d\n", myList.size);
int i;
for( i = 0; i < 10; i++)
{
insert(&myList, i);
}
printf("The size is %d\n", myList.size);
printf("%d\n", (myList.head)->data);
printf("%d\n", myList.head);
printList(&myList);
}
So here's my code. I am trying to implement a linked list. The main problem I have is with the printList. It only prints memory locations (as well as the other printf's in main that I added as tests). I've tried (*current).data which still only prints memory locations and I've tried *(current->data) which returns an error when I compile. After inserting the five, the printlist runs once, printing one memory location as expected, but then when I insert 10 more values in the for loop, then print again, I get an infinite loop and it ends up printing the same address infinitely. I tried adding that k so the printList will print a maximum of 50 times to prevent the infinite loop and that compiles fine, but when I run, the program prints one memory location (presumably the first printList) and then I get a Bus Error.
I am already a java programmer, but C is really giving me a hell of a time.