If any more questions don’t doubt to contact me. Good luck.
About createlist()
Head should point to the first element of the list, but you are using head to instantiate the newest element added to the list. First and head conceptually are the same. I think you think head should be the last element of the list but it should’t. Read more about linked lists. Also you are pointing to head from temp? But wait, what is temp? Can you tell me what is temp at that point in your program? You should’t rely on temp, that’s why it is called temp. It should be a temporal variable. Not a global variable.
About read_list()
Why temp-> next = 0? You could be breaking your chain here because we don’t know what temp is. Let’s continue. So you point to the first element in the list (which should be the same as head). OH MY GOD! You point first to null. Of course you can’t read the list twice nor thrice. Once you point first to null you lose your only reference to the first element of the list. It doesn’t matter what you do with your other functions. You won’t be able to call this function twice in a row and your code will break.
I mean. First time you pass you do temp = first. Then first = NULL
Second time. temp = first. Buut… first = null? Rings a bell? You won’t be able to print anything else.
The while loop is ok.
About delete_list()
Ok you lost me here. You are using head now. But i can tell you, head is absolutely not pointing to the first element in the list. If anything it is the last element on the list and (head->next = random stuff) and if lucky (head->next = null). I am not sure, but if head would point to the first element on the list (but it is not) this function may be ok or close to ok. But don’t take my word on this one, i am really tired right now.
Summary:
Your functions are wrong. There is not one function breaking your code but many. Your main problem is that you are messing up things with the pointers. If i were you i would:
- Remove first and temp as global variables.
- Review linked lists Linked Lists
- Use temp as a local variable in your functions.
- Freed allocated memory and if possible check for errors when allocating.
- Head should always point to the first element in the list. You should’t update head, unless you add the first element to the list or remove the last one.
- The only case where you should update head is when adding an element to the beginning of the list but i encourage you to try making the program without this feature first.
If you allocate memory (for ex, calloc or malloc) you should always freed that memory and point the pointers to NULL (It is good practice). The way you programmed you are leaking memory.
Also you should’t have temp as a global variable. It can produce problems you wouldn’t have if you weren’t using it. Also having three global variables makes things harder to review or debug. When the only global variable you should have is head.
I wrote an alternative for your code if you want to check it.
Explanation of the code:
I don’t check for user input but it can be easily added in the main function.
There are 5 functions: addLastElement, addFirstElement, deleteList, deleteElement and readList.
-
readList: Prints the list.
-
addLastElement: IF there is no element it will add the head. If there
are elements, it will add a new element at the end of the list. It
doesn’t update the head unless head is null. -
addFirstElement: Adds an element at the beginning of the list. This
function always updates the head. -
deleteElement: Deletes element in the position passed as an argument.
And it frees the memory. -
deleteList: Will delete all elements from the list and free the
memory.
All the functions except for readList will update the variable “size” of the struct list accordingly.
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct student
{
int id;
int age;
struct student *next;
};
struct list
{
struct student *head;
int size;
};
void addFirstElement(struct list *list, int id, int age)
{
struct student *temp;
temp = (struct student *)malloc(sizeof(struct student));
if (temp == NULL)
{
printf("Error: Couldn't allocate memory\n");
}
temp -> id = id;
temp -> age = age;
temp->next = list -> head;
list->head = temp;
list->size++;
}
void addLastElement(struct list *list, int id, int age)
{
struct student *new;
struct student *temp;
new = (struct student *)malloc(sizeof(struct student));
if (new == NULL)
{
printf("Error: Couldn't allocate memory\n");
}
new->id = id;
new->age = age;
new->next = NULL;
if (list->head == NULL)
{
list->head = new;
}
else
{
temp = list -> head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new;
}
list->size++;
}
void readList(struct list *list)
{
struct student *temp = list->head;
while (temp != NULL)
{
printf("ID: %d - Age: %d\n", temp->id, temp->age);
temp = temp->next;
}
}
void deleteList(struct list *list)
{
struct student *temp;
struct student *previous;
temp = list->head;
while (temp != NULL)
{
previous = temp;
temp = temp->next;
free(previous);
}
previous = NULL;
list->size = 0;
list->head = NULL;
}
void deleteElement(struct list *list, int position)
{
if (list->head == NULL)
return;
if (position < 0 || position > ((list->size) - 1))
{
printf("The list is not that big\n");
return;
}
struct student *temp = list->head;
struct student *previous = NULL;
int i = 0;
temp = list->head;
while (i != position)
{
previous = temp;
temp = temp->next;
i++;
}
if (previous == NULL)
{
list->head = list->head->next;
}
else
{
previous->next = temp->next;
}
list->size--;
free(temp);
temp = NULL;
return;
}
int main()
{
struct list list;
int i;
list.size = 0;
list.head = NULL;
for (i = 0; i < 10; i++)
{
addLastElement(&list, i, (i+1)*10);
}
deleteElement(&list, 5);
addFirstElement(&list, 100, 50);
readList(&list);
deleteList(&list);
return 1;
}
Output:
ID: 100 - Age: 50
ID: 0 - Age: 10
ID: 1 - Age: 20
ID: 2 - Age: 30
ID: 3 - Age: 40
ID: 4 - Age: 50
ID: 6 - Age: 70
ID: 7 - Age: 80
ID: 8 - Age: 90
ID: 9 - Age: 100
solved C programming error in code [closed]