i have this code
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct node *next, *previous;
}Node;
typedef struct LinkedList
{
struct Node *head, *tail;
}LinkedList;
void createhead(LinkedList *list);
int getdata(void);
Node *getmemory(void);
void appendlist(LinkedList *list);
void printlist(const LinkedList list);
void deletelist(LinkedList *list);
int main()
{
LinkedList list;
createhead(&list);
appendlist(&list);
appendlist(&list);
appendlist(&list);
printlist(list);
deletelist(&list);
return 0;
}
void createhead(LinkedList *list)
{
Node *tmpnode = NULL;
list->head = NULL;
list->tail = NULL;
tmpnode = getmemory();
if (!tmpnode)
{
printf("error getting memory");
exit(EXIT_FAILURE);
}
tmpnode->data = getdata();
tmpnode->previous = NULL;
tmpnode->next = NULL;
list->head = tmpnode;
list->tail = list->head;
}
int getdata(void)
{
int data;
printf("Please enter a number: ");
scanf(" %d", &data);
return data;
}
Node *getmemory(void)
{
return malloc(sizeof(Node));
}
void appendlist(LinkedList *list)
{
Node *tmpnode = NULL;
tmpnode = getmemory();
if (!tmpnode)
{
printf("error getting memory");
return;
}
tmpnode->data = getdata();
tmpnode->next = NULL;
list->tail->next = tmpnode;
list->tail = tmpnode;
if (!list->head->next)
{
list->head->next = tmpnode;
}
}
void printlist(const LinkedList list)
{
int count = 1;
Node *current = list.head;
while (!current)
{
printf("node %d's data is %d", count, current->data);
count++;
current = current->next;
}
}
void deletelist(LinkedList *list)
{
int count = 1;
Node *tmpnode = NULL;
while (list->head)
{
tmpnode = list->head->next;
free(list->head);
printf("node %d freed", count);
count++;
list->head = tmpnode;
}
}
lines 86 90 103 and 114 i get the warning yet i have done no different than what i have done previously here post 1 line 146 linked list sorting function causes segfault
has the wind changed direction again without me noticing??