Been studying data structures in C to get more grasp on the fundamentals as a freshman at uni. This is a coding-challenge that I've given to myself to understand Linked Lists better.
Basically, my aim was to create 2 nodes, along with the node.header that i've written, which has the functions to add nodes to the head, body, or tail of the list.
And another function in that header to combine these two linked lists together into one.
How can I get this code better, do you see any mistakes or improvements that can be done with the working code? I think I got a better grasp on pointers more after coding this and planning to go deeper.
The code is already working, though there are bugs that I still am working on understanding pointers more and memory allocation,
such as free() function when used in the header gets my program go wild and not setting nextptr as null, i've mentioned this because they are commented in the code, as not working.
Main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
void modify_nodes(struct node *list,struct node **head);
void create_nodes(struct node **head);
int main(){
struct node *head = NULL;
create_nodes(&head);
puts("You're modifying the First one");
modify_nodes(head,&head);
struct node *head2 = NULL;
create_nodes(&head2);
puts("You're modifying the Second one");
modify_nodes(head2,&head2);
concatenate_lists(&head2,&head);
print_list(head); //head2 doesnt work out here because i can't check out if there is a previous node or not.
}
void modify_nodes(struct node *list,struct node **head){
enum choose_menu menu;
int input_value;
printf("To Add a Node to the Head, Press 1\n"
"To Add a Node to the Middle, Press 2\n"
"To Add a Node to the End, Press 3\n"
"To Finish the process, Press 0\n\n"
);
scanf("%d",&input_value);
while(input_value != END_OPERATION){
scanf("%d",&input_value);
if(input_value == ADD_HEAD){
add_node_head(&list);
}else if(input_value == ADD_MIDDLE){
puts("Please enter at which place you want your node to be...");
scanf("%d",&input_value);
add_node_middle(&list,input_value);
}else if(input_value == ADD_LAST){
add_node_last(&list);
}
}
*head = list;
}
void create_nodes(struct node **head){
(*head) = (struct node*)malloc(sizeof(struct node));
(*head)->value = 'x';
(*head)->nextPtr = NULL;
}
node.h
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
enum choose_menu{ADD_HEAD = 1, ADD_MIDDLE = 2, ADD_LAST = 3, END_OPERATION = 0};
struct node{
char value;
struct node *nextPtr;
};
void print_list(struct node *firstNode){
while(firstNode != NULL){
printf("%c--->",firstNode->value);
firstNode = firstNode->nextPtr;
}
printf("NULL");
}
void add_node_head(struct node **head){
struct node *add = NULL;
add = (struct node*)malloc(sizeof(struct node));
add->value = 'a';
add->nextPtr = *head;
*head = add;
//free(add);
}
void add_node_last(struct node **head){
struct node *new_ = NULL;
new_ = (struct node*)malloc(sizeof(struct node));
new_->nextPtr=NULL;
new_->value = 'b';
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp = *head;
while(temp->nextPtr != NULL){
temp = temp->nextPtr;
}
temp->nextPtr = new_;
/* free(temp);
free(new_);*/
}
void add_node_middle(struct node **head,int position){
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->value = 'c';
struct node *temp = *head;
for(int i=2; i<position; i++){
if(temp->nextPtr != NULL){
temp = temp->nextPtr;
}
}
newNode->nextPtr = temp->nextPtr;
temp->nextPtr = newNode;
//free(newNode);
}
void concatenate_lists(struct node **adding,struct node **beginning){
struct node *temp = NULL;
temp = (struct node*)malloc(sizeof(struct node));
struct node *head = NULL;
head = (struct node*)malloc(sizeof(struct node));
temp = *beginning;
head = temp;
while(temp->nextPtr != NULL){
temp = temp->nextPtr;
}
temp->nextPtr = *adding;
*beginning = head;
/*free(temp);
free(head);*/
}
#endif // NODE_H_INCLUDED
malloc()
has a cast. Why did you cast vs. leaving the cast off? \$\endgroup\$