I am trying to create a linked list with various options and, unfortunately, stuck at the process of inserting elements(hour and minute) at the beginning of a linked list. My code is:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct node{
int hour, minute;
struct node* next;
}node;
void insert_beginning (node** root1, node** root2, int value) {
node* new_node = malloc(sizeof(node));
if (new_node == NULL) {
exit(1);
}
new_node->hour = value;
new_node->next = *root1;
new_node->minute = value;
new_node->next = *root2;
*root1 = new_node;
*root2 = new_node;
}
int main (int argc, char* argv[]) {
int option = 0;
node* root = NULL;
printf("Choose option: \n");
printf("1. Add time to the list. \n");
printf("2. Delete time from the list. \n");
printf("3. Change the postion of the bisst and the smallest elements. \n");
printf("4. Write the list. \n");
printf("5. Delete the list. \n");
printf("Your option: ");
scanf("%i", &option);
if (option == '1')
{
node* root1 = NULL;
node* root2 = NULL;
insert_beginning(&root1, &root2, 12, 15);
insert_beginning(&root1, &root2, 13, 20);
insert_beginning(&root1, &root2, 14, 25);
for (node* curr = root; curr != NULL; curr = curr->next)
{
printf("%d:%d\n", curr->hour, curr->minute);
}
}
return 0;
}
My problem is in these lines (I think):
insert_beginning(&root1, &root2, 12, 15);
insert_beginning(&root1, &root2, 13, 20);
insert_beginning(&root1, &root2, 14, 25);
**too many arguments to function call, expected 3, have 4**
But also I am afraid it does not compile from the beginning. (An example):
Choose option:
1. Add time to the list.
2. Delete time from the list.
3. Change the postion of the bisst and the smallest elements.
4. Write the list.
5. Delete the list.
Your option: 1
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
The goal is to receive a linked list like this (example):
Choose option:
1. Add time to the list.
2. Delete time from the list.
3. Change the postion of the bisst and the smallest elements.
4. Write the list.
5. Delete the list.
Your option: 1
14:25
13:20
12:15
I would highly appreciate your help!
CodePudding user response:
- Your program doesn't compile as
insert_beginning()
takes twonode **
and avalue
but you call it with 4 arguments as noted by your compiler. You probably want 3 argumentsnode **
,int hour
andint minute
. - You read
int
option
but compare it against the character value ('1'). - You have 3 root variables
main()
. You probably only want one. - In you first call
*root1
and*root2
areNULL
which will cause a segfault when you dereference those. - Always check the return value of
scanf()
otherwise you may be operating on uninitialized values. In your code you actually initializeoption = 0
but your prompts suggest that you don't handle that case.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct node{
int hour;
int minute;
struct node* next;
} node;
void insert_beginning (node** root, int hour, int minute) {
node* new_node = malloc(sizeof(node));
if (!new_node) {
exit(1);
}
new_node->hour = hour;
new_node->minute = minute;
if(!*root) {
*root = new_node;
(*root)->next = NULL;
return;
}
new_node->next = *root;
*root = new_node;
}
int main (int argc, char* argv[]) {
node* root = NULL;
printf("Choose option: \n");
printf("1. Add time to the list. \n");
printf("2. Delete time from the list. \n");
printf("3. Change the postion of the bisst and the smallest elements. \n");
printf("4. Write the list. \n");
printf("5. Delete the list. \n");
printf("Your option: ");
int option;
if(scanf("%i", &option) != 1) {
printf("scanf failed\n");
exit(1);
}
if (option == 1) {
insert_beginning(&root, 12, 15);
insert_beginning(&root, 13, 20);
insert_beginning(&root, 14, 25);
for (node* curr = root; curr; curr = curr->next) {
printf("%d:%d\n", curr->hour, curr->minute);
}
}
}
and example output (note last in first out order as implies by "insert_beginning"):
Choose option:
1. Add time to the list.
2. Delete time from the list.
3. Change the postion of the bisst and the smallest elements.
4. Write the list.
5. Delete the list.
Your option: 1
14:25
13:20
12:15