Segfault while printing linked list after stack clear - c

To be more specific, this code is supposed to be a lesser clone of the Unix function dc. The linked list seems to be working fine. If I attempt to use c to clear the memory, add more numbers, then print again with f I get a segfault. It seems to be printing what should be the Null Node in the linked list.
Interaction:
$ ./test
1 2 3
f
3
2
1
c
4 5
f
5
4
0
Segmentation Fault
Here's the code itself:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int val;
struct Node *next;
};
void cons_node(struct Node **head, int num)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->val = num;
newNode->next = NULL;
if (*head == NULL){
*head = newNode;
}
else {
newNode->next = *head;
*head = newNode;
}
}
I'm assuming the problem lies here in the display func:
void display_list(struct Node *head)
{
struct Node *current = head;
while(current != NULL)
{
printf("%d\n", current->val);
current = current->next;}
}
void print_top(struct Node *head)
{
printf("%d\n", head->val);
}
or here in the clear func:
void clear_stack(struct Node *head)
{
struct Node *current;
while ((current = head)!= NULL) {
head = head->next;
free(current);
}
}
void vorpal_func(struct Node *head)
{
struct Node *current;
current = head;
free(current);
}
int main(){
int input;
int first = 1;
char quit = 'n';
char inputchar = ' ';
struct Node *head = NULL;
while (quit == 'n'){
if (scanf("%d", &input) == 1){
cons_node(&head, input);
first = 0;
}
else{
inputchar = getchar();
if(first == 1)
printf("List is empty\n");
else{
switch (inputchar){
case 'q':
quit = 'y';
break;
case 'E':
quit = 'y';
break;
case 'f':
display_list(head);
break;
case 'p':
print_top(head);
break;
case 'c':
clear_stack(head);
first = 1;
break;
case 't':
vorpal_func(head);
break;
}
}
}
}
return 0;
}
I've been attempting to figure out the problem for a few hours now. Any tips?

You don't clear your head after calling clear_stack, so when you add your next node, the next pointer is set to something that points into memory that has been freed. Or you could pass a pointer to head to clear_stack if you preferred.
void clear_stack(struct Node **head)
{
while (*head != NULL)
{
Node *current = *head;
*head = current->next;
free(current);
}
}
In passing, cons_node could be written like this
void cons_node(struct Node **head, int num)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->val = num;
newNode->next = *head;
*head = newNode;
}

Related

I am Working with Doubly Linked Lists in C and I am Using Turbo C++ but The Compiler is Taking Two Additional Nodes without Adding

I am Working With Doubly Linked List & Implementing Them using C
I am using Turbo C++ as my Compiler
But it's Taking Two Constant Additional Nodes Every Time without Writing Code for It
The Same Code is Running in VS Code
But I Should Run it In Turbo C++
I tried Changing Systems, but it didn't Work
'''
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Node
{
struct Node *prev;
int data;
struct Node *next;
} *head = NULL, *temp = NULL;
void insatbeg()
{
int item;
struct Node *ptr = NULL;
printf("\nEnter Item: ");
scanf("%d", &item);
ptr = (struct Node *)malloc(sizeof(struct Node *));
if (ptr == NULL)
printf("\nOverflow Occured");
else if (head == NULL)
{
ptr->data = item;
ptr->next = ptr->prev = NULL;
head = ptr;
}
else
{
ptr->prev = NULL;
ptr->data = item;
ptr->next = head;
head->prev = ptr;
head = ptr;
}
}
void display()
{
if (head == NULL)
printf("\nList is Empty");
else
{
temp = head;
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
}
}
int main()
{
int loopvar = 1, switchvar;
clrscr();
code:
while (loopvar == 1)
{
printf("\nEnter 1 to Insert at First");
printf("\nEnter 2 to Display");
printf("\nEnter: ");
scanf("%d", &switchvar);
switch (switchvar)
{
case 1:
insatbeg();
break;
case 2:
display();
break;
default:
printf("\nEnter Properly: ");
goto code;
break;
}
printf("\nDo You Want to Continue: ");
scanf("%d", &loopvar);
}
getch();
}
'''
Also the Redundant Nodes Added Always are Constant
This memory allocation
ptr = (struct Node *)malloc(sizeof(struct Node *));
is invalid. You allocated a memory for the pointer type struct Node * while you need to allocate memory for an object of the type struct Node. So write
ptr = (struct Node *)malloc(sizeof(struct Node));
Also in this code snippet
else
{
ptr->prev = NULL;
ptr->data = item;
ptr->next = head;
head = ptr;
}
you forgot to set the data member prev of the head node. Write
else
{
ptr->prev = NULL;
ptr->data = item;
ptr->next = head;
head->prev = ptr;
head = ptr;
}
Pay attention to that the pointer temp1 is not used in your program.
If the code does not work as expected using the compiler Turbo C++ then try to initialize the pointer to the head node explicitly
struct Node
{
struct Node *prev;
int data;
struct Node *next;
} *head = NULL, *temp, *temp1;

unable to insert into ordered linked list in C

i am new to programming and C. I am trying to create an ordered linked list. For some reason which i cannot figure out, it never enters the first if block in the insert_in_order function even though my linked list is empty when i call the insert_in_order function. Would anyone have any idea what i am doing wrong?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node* next;
};
typedef struct node node_t;
void printlist(node_t *head){
node_t *temporary = head;
while(temporary != NULL){
printf("%d - ", temporary->value);
temporary = temporary->next;
}
printf("\n");
}
node_t *create_new_node(int value){
node_t *result = malloc(sizeof(node_t));
result->value = value;
result->next = NULL;
//printf("result.value = %d\n", result->value);
return result;
}
void insert_in_order (node_t *head, node_t *node_to_insert){
node_t *current_node = head;
node_t *prior_node = head;
//linked list is empty
if (head == NULL){ //never enters if block for some reason
head->next = node_to_insert;
node_to_insert->next = NULL;
break;
//printf("inside the if stmt");
}
if(node_to_insert->value <= current_node->value){
head->next = node_to_insert;
node_to_insert->next = current_node;
break;
}
current_node = current_node->next;
while (current_node->next != NULL){
if(node_to_insert->value <= current_node->value){
node_to_insert->next = current_node;
prior_node->next = node_to_insert;
break;
}
else if (node_to_insert > current_node){
current_node = current_node->next;
prior_node = prior_node->next;
}
}
//node to insert is the largest in the linked list
current_node->next = node_to_insert;
node_to_insert->next = NULL;
}
int main(){
node_t *head;
node_t *node1;
node_t *node2;
head = NULL;
node1 = create_new_node(22);
node2 = create_new_node(33);
printf("node1's value equals %d\n", node1->value);
printf("node2's value equals %d\n", node2->value);
insert_in_order(head, node1);
printlist(head);
}
First of all this code does not compile - these breaks are invalid
if (head == NULL){ //never enters if block for some reason
head->next = node_to_insert;
node_to_insert->next = NULL;
break; <<<<====
//printf("inside the if stmt");
}
and
if (node_to_insert->value <= current_node->value) {
head->next = node_to_insert;
node_to_insert->next = current_node;
break; <<<=====
}
Seems like you meant return when you said break, now compiles with those replaced by return
Now this goes wrong
//linked list is empty
if (head == NULL) { //never enters if block for some reason
head->next = node_to_insert;
You just tested to see if head is NULL and if it is you try to use it, thats never going to work
You mean this
//linked list is empty
if (head == NULL) { //never enters if block for some reason
head = node_to_insert;
node_to_insert->next = NULL;
return;
}
code now runs to completion, although there may be other errors
node1's value equals 22
node2's value equals 33

linked list using double pointers

Using double pointers first time to create and display linked list
#include "stdio.h"
#include "stdlib.h"
struct node
{
int data;
struct node * next;
};
void Insert(struct node **, int , int );
void display(struct node *);
int main()
{
int c, data, position;
struct node* head;
do{
printf("Enter a choice :\n");
printf("1. Add an element.\n");
printf("2. Del an element.\n3.Display List.\n");
printf("4.Delete linked list.\n5.Exit.\n");
printf("Your Choice :");
scanf("%d",&c);
switch(c){
case 1 :
printf("\nEnter data and position :\n");
scanf("%d %d",&data,&position);
Insert(&head,data,position);
break;
case 2 :
break;
case 3 :
printf("Linked List : \n");
display(head);
break;
case 4 :
break;
case 5 :
exit(0);
default :
printf("Invalid Choice.\n");
break;
}
}while(1);
return 0;
}
void Insert(struct node **ptrhead, int item, int position){
struct node *p,*newnode;
//node creation.
newnode = (struct node *)malloc(sizeof(struct node));
if (!newnode)
{
printf("Memory Error.\n");
return;
}
newnode->next = NULL;
newnode->data = item;
p = *ptrhead;
// Creates initial node
if (!(p->data))
{
p = newnode;
}
// insertion at beginning
if (position==1)
{
newnode->next = p;
p = newnode;
free(newnode);
}
// insertionn at middle or end.
else
{
int i=1;
while(p->next!=NULL && i<position-1){
p=p->next;
i++;
}
newnode->next = p->next;
p->next = newnode;
}
*ptrhead = p;
};
// Display Linked list
void display(struct node *head){
if (head)
{
do{
printf("%d\n", head->data);
head = head->next;
}while(head->next);
}
};
I will add functions for deletion and other operations later. Right now , I just want to insert and display fns to work . But output comes as infinitely running loop with wrong values. I cannot figure out what's wrong in my code , please help ?
Thanks in advance.
Not sure why somebody would be writing this type of C today, looks like maybe I'm doing your homework for you... In any case, you asked to fix your code, not rewrite it, so here's the minimum set of changes.
head should be initialized to NULL.
if (!(p->data)) is not right. That if statement should just be:
// Creates initial node
if (!p)
{
*ptrhead = newnode;
return;
}
Remove free(newnode);.
The insert at middle/end code could be
int i = 1;
struct node *n = p;
while (n->next != NULL && i<position - 1){
n = n->next;
i++;
}
newnode->next = n->next;
n->next = newnode;
The final insert function:
void Insert(struct node **ptrhead, int item, int position)
{
struct node *p, *newnode;
//node creation.
newnode = (struct node *)malloc(sizeof(struct node));
if (!newnode)
{
printf("Memory Error.\n");
return;
}
newnode->next = NULL;
newnode->data = item;
p = *ptrhead;
// Creates initial node
if (!p)
{
*ptrhead = newnode;
return;
}
// insertion at beginning
if (position == 1)
{
newnode->next = p;
p = newnode;
}
// insertionn at middle or end.
else
{
int i = 1;
struct node *n = p;
while (n->next != NULL && i<position - 1){
n = n->next;
i++;
}
newnode->next = n->next;
n->next = newnode;
}
*ptrhead = p;
}
Your print function isn't quite right, just make it:
// Display Linked list
void display(struct node *head)
{
while (head)
{
printf("%d\n", head->data);
head = head->next;
}
}

LinkedList functions crash in c

I implemented a linked list in c. when i test it, whenever i reach display function or insert at the end the program crashes. These are the functions:
struct Node
{
char data;
struct Node *next;
};
struct LinkedList
{
struct Node *head;
};
void insertAtBeginning(struct LinkedList *LL, char ele)
{
struct Node *new = (struct Node*)malloc(sizeof(struct Node));
new->data = ele;
new->next = NULL;
if(new->data == '\n')return;
if(LL->head==NULL)LL->head = new;
else
{
new->next=LL->head;
LL->head=new;
}
}
void insertAtTheEnd(struct LinkedList *LL, char ele)
{
struct Node *new = (struct Node*) malloc(sizeof(struct Node));
new->data = ele;
new->next = NULL;
if(LL->head==NULL){LL->head=new;return;}
struct Node *current = LL->head;
while(current->next != NULL) {current = current->next;}
current->next = new;
}
void deleteNode(struct LinkedList* LL, char ele)
{
struct Node *current = LL->head;
struct Node *temp = LL->head;
while(current->next->data!=ele && current!=NULL)current=current->next;
temp=current->next;
current->next=current->next->next;
free(temp);
}
void deleteFirstNode(struct LinkedList* LL)
{
struct Node *temp;
if(LL->head != NULL)
{
temp = LL->head;
LL->head = LL->head->next;
free(temp);
}
}
void displayLinkedList(struct LinkedList LL)
{
struct Node *current = LL.head;
printf("List: ");
while(current != NULL)
{
printf("%c",current->data);
current = current->next;
}
printf("\n");
}
This is the main:
main()
{
struct LinkedList LL;
char c = '0';
//Inserting at beginning
printf("Type a string. Press Enter to end: ");
while(c != '\n')
{
scanf("%c",&c);
insertAtBeginning(&LL, c);
}
printf("List: ");
displayLinkedList(LL);
printf("\n");
//Inserting at end
c='0';
printf("Type a string. Press Enter to end: ");
while(c != '\n')
{
scanf("%c",&c);
insertAtTheEnd(&LL, c);
}
printf("List: ");
displayLinkedList(LL);
printf("\n");
//Remove
printf("Enter a char to remove: ");
scanf("%c",&c);
deleteNode(&LL, c);
printf("\n");
printf("List: ");
displayLinkedList(LL);
printf("\n");
deleteFirstNode(&LL);
printf("List: ");
displayLinkedList(LL);
}
of course This is done after inserting the necessary libraries.
you have to set NULL to next when you create element. if you do not do it, your while loop behaviour will be buggy
void insertAtTheEnd(struct LinkedList *LL, char ele)
{
struct Node *new = (struct Node*) malloc(sizeof(struct Node));
new->next = NULL;
new->data = ele;
if(LL->head == NULL) LL->head = new;
else
{
struct Node *current = LL->head;
while(current->next != NULL) {current = current->next;}
current->next = new;
}
}
, and do same for other functions.
also you have to set NULL to your LinkedList.head. so, set NULL when you define LinkedList
struct LinkedList LL;
LL.head = NULL;
i asume the line if(current = LL->head) is wrong
bacause temp = current->next would crash if the result is NULL
otherwise the code after the if statement doesnt makes sense
There are some instances where you will try to insert at the end. When you have something of the form node_new->next->prev = node, you must check to make sure that node_new->next is not NULL, because NULL does not have any type and thus does not have a prev field.
You may also consider making all of your functions return int, so that you can know if and what goes wrong.

Why are my linked list modifications not working in this c code?

This is my code, and I'm trying to add, print, delete, empty the list with a menu with user input but it doesn't hold or ouput any values. I debugged it by calling the functions outside of the loop and they work but the problem is that the calls don't output anything inside the loop.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int Length(struct node* head);
void PrintList(struct node* head);
void Add(struct node** headRef, int new );
int Delete(struct node** headRef);
void ZeroList(struct node** headRef);
int main(void) {
struct node * head = NULL;
char enter;
int x;
Add( &head, 13 );
printf("\na(add){x}\nd(del)\nl(ength)\np(rint)\nz(ero)\ne(xit)");
do
{
fscanf(stdin, "%c", &enter);
struct node *head = NULL;
switch (enter)
{
case 'a':
printf("Enter a node: ");
fscanf(stdin,"%d", &x);
Add(&head, x);
break;
case 'd':
printf("Delete\n");
Delete(&head);
break;
case 'l':
printf("Length");
Length(head);
break;
case 'p':
printf("printList");
PrintList(head);
break;
case 'z':
printf("ZeroList");
ZeroList(&head);
break;
}
}while (enter != 'e');
Add(&head, 23);
PrintList(head);
return 0;
}
//Debug
/* Add( &head, 3 );
Add( &head, 20 );
Add( &head, 55 );
Delete(&head);
Length(head);
PrintList(head);
ZeroList(&head);
PrintList(head);*/
int Length(struct node* head) {
struct node *current = head;
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
printf(" Size of head is %d\n", count);
return(count);
}
void PrintList(struct node* head) {
struct node *current = head;
while (current != NULL)
{
printf("printing %d\n", current->data);
current = current->next;
}
}
void Add(struct node** headRef, int new) {
struct node *k = malloc(sizeof(struct node));
k->data = new;
k->next = *headRef;
*headRef = k;
return;
}
int Delete(struct node** headRef) {
struct node* current = *headRef;
if (current == NULL)
{
printf("List is empty!\n");
}
else
{
printf("Deleted value is: %d\n", current->data);
*headRef = current->next;
free(current);
}
return 0;
}
void ZeroList(struct node** headRef){
struct node* current = *headRef;
while (current != NULL)
{
current = current->next;
free(current);
}
}
At this point in your loop:
case 'a':
printf("Enter a node: ");
fscanf(stdin,"%d", &x);
You have to add the Add-function:
Add(&head, x);
This can't be inside the loop, remove it:
struct node *head = NULL;
You also have a two errors in ZeroList. You never free the first element, and you free NULL at the end of the list. You might try this instead:
void ZeroList(struct node** headRef){
struct node* current = *headRef;
while (current != NULL)
{
struct node* tmp = current;
current = current->next;
free(tmp);
}
*headRef=NULL;
}
This is my test-run of your program:
a(add){x}
d(del)
l(ength)
p(rint)
z(ero)
e(xit)a
Enter a node: 1
a
Enter a node: 2
a
Enter a node: 3
p
printListprinting 3
printing 2
printing 1

Resources