Garbage value for certain C functions - c

I'm learning C and I'm trying to make a program with multiple linked list operations all at once. They don't give me errors, but for the functions to delete a node at the end, and to insert at a certain position, I'm getting a garbage value at the beginning of the output.
Temporary program to implement just the insert function:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void inp(struct node **start, int ele)
{
struct node *NEW = (struct node*)malloc(sizeof(struct node));
NEW->data = ele;
struct node *ptr = *start;
while(ptr != NULL)
{
if(ptr->next == NULL)
{
ptr->next = NEW;
NEW->next = NULL;
break;
}
ptr = ptr->next;
}
}
void insert_pos(struct node **start, int ele, int pos)
{
struct node *ptr = *start;
for(int i = 1; i < pos; i++)
{
ptr = ptr->next;
}
struct node *NEW = (struct node*)malloc(sizeof(struct node));
NEW->data = ele;
NEW->next = ptr->next;
ptr->next = NEW;
}
void display(struct node* start)
{
struct node* ptr = start;
while(ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
int main()
{
struct node *a;
int m;
for(int i = 0; i < 10; i++)
{
scanf("%d", &m);
inp(&a, m);
}
insert_pos(&a, 594, 3);
printf("\n\n");
display(a);
}
Temporary program to delete at the end:
//Temporary
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void inp(struct node **start, int ele)
{
struct node *NEW = (struct node*)malloc(sizeof(struct node));
NEW->data = ele;
struct node *ptr = *start;
while(ptr != NULL)
{
if(ptr->next == NULL)
{
ptr->next = NEW;
NEW->next = NULL;
break;
}
ptr = ptr->next;
}
}
void del_end(struct node **start)
{
struct node *ptr = *start;
while(ptr != NULL)
{
if(ptr->next->next == NULL)
{
ptr->next = NULL;
free(ptr->next->next);
break;
}
ptr = ptr->next;
}
}
void display(struct node* start)
{
struct node* ptr = start;
while(ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
int main()
{
struct node *a;
int m;
for(int i = 0; i < 10; i++)
{
scanf("%d", &m);
inp(&a, m);
}
del_end(&a);
printf("\n\n");
display(a);
}
Any idea how to fix it?

you can see that in here:
The inp problem:
struct node *a;
puts a garbage value in a that you don't handle and uses it like a node,
add =NULL to solve.
also, your function that deletes from the end has the following error:
struct node *ptr = *start;
while(ptr != NULL)
{
if(ptr->next->next == NULL)
{
ptr->nexStack Overflow for Teams – Collaborate and share knowledge with a private group.t = NULL;
free(ptr->next->next);
break;
}
ptr = ptr->next;
}
you are changing ptr-> next and than accessing ptr->next->next...

Related

Linked List - Implementation

I'm basically trying to implement LinkedList in C but I still can't get it I will leave my code below so you can see what I'm missing:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int val;
struct Node *Next;
};
struct Node *head;
void insert(int x);
void print();
int main() {
head = NULL;
int d, i = 0, f;
printf("How many number you want to insert:\n");
scanf("%d", &d);
for (i; i < d; i++) {
printf("Enter your number:\n");
scanf("%d", &f);
insert(f);
print();
}
return 0;
}
void insert(int x) {
struct Node *A = (struct Node *)malloc(sizeof(struct Node));
if (head == NULL) {
A->val = x;
A->Next = head;
head = A;
return;
}
A->val = x;
A->Next = head->Next;
}
void print() {
printf("[");
struct Node *B = head;
while (B->Next != NULL) {
B = B->Next;
printf("%d", B->val);
}
printf("]\n");
}
I need some pointers to where I missed up.
Here are the two ways to insert, either at the front/head of the list or the back/tail of the list:
void
insert_front(int x)
{
struct Node *A = malloc(sizeof(*A));
A->val = x;
A->Next = head;
head = A;
}
void
insert_back(int x)
{
struct Node *A = malloc(sizeof(*A));
A->val = x;
A->Next = NULL;
struct Node *prev = NULL;
for (struct Node *cur = head; cur != NULL; cur = cur->Next)
prev = cur;
if (prev != NULL)
prev->Next = A;
else
head = A;
}
The print() function is incorrect: it misses the first node and would crash on an empty list:
void print() {
printf("[");
struct Node *B = head;
while (B != NULL) {
printf(" %d", B->val);
B = B->Next;
}
printf(" ]\n");
}
The insert() function also has problems: only the first node is correctly inserted. Here is a modified version:
// insert a node in front of the list.
void insert(int x) {
struct Node *A = (struct Node *)malloc(sizeof(struct Node));
if (A != NULL) {
A->val = x;
A->Next = head;
head = A;
}
}

Why am I getting a segmentation fault on my bubble sort?

The bubble sort in my code works, but when the program goes to print the newly sorted list I get a segmentation fault.
I print out the swap sequence and it shows that it is correct. The program segmentation faults after the sorting happens and it goes to print the list in order. I'm not sure exactly whats going wrong here.
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t {
int num;
struct node_t *next;
struct node_t *prev;
} node_t;
void add_node (struct node_t **head, int num) {
struct node_t *new = (struct node_t*)malloc(sizeof(struct node_t));
struct node_t *last = *head;
new->num = num;
new->next = NULL;
if (*head == NULL) {
new->prev = NULL;
*head = new;
return;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = new;
new->prev = last;
}
return;
}
void swap_num(struct node_t **first, struct node_t **second) {
struct node_t *temp;
printf("%d %d\n", (*first)->num, (*second)->num);
temp->num = (*first)->num;
(*first)->num = (*second)->num;
(*second)->num = temp->num;
}
void sort(struct node_t **head) {
int swapped;
struct node_t *temp;
if (*head == NULL){
printf("list is empty...\n");
return;
}
do {
swapped = 0;
temp = *head;
while (temp->next != NULL) {
if (temp->num > temp->next->num) {
swap_num(&temp, &(temp->next));
swapped = 1;
}
temp = temp->next;
}
} while (swapped);
}
void print_list (struct node_t **head) {
struct node_t *temp;
if (*head != NULL) {
temp = *head;
while (temp != NULL) {
printf("%d ", temp->num);
temp = temp->next;
}
printf("\n");
}
}
int main (void) {
struct node_t *head = NULL;
int new_num, x, y, kill;
while (new_num != 0) {
scanf("%d", &new_num);
if (new_num != 0) {
add_node(&head, new_num);
print_list(&head);
}
}
print_list(&head);
sort(&head);
printf("------------------\n");
print_list(&head);
return 0;
}
This seems to be your problem right here:
..\main.c: In function 'swap_num':
..\main.c:40:15: error: 'temp' is used uninitialized in this function [-Werror=uninitialized]
temp->num = (*first)->num;
~~~~~~~~~~^~~~~~~~~~~~~~~
I got this using compile options such as -Wall, -Wextra, and -Werror. If I fix that, your code does not crash. To fix it I just used an int temporary to hold the value instead of a struct node_t*. Here's my revised swap_num() function:
void swap_num(struct node_t **first, struct node_t **second)
{
int temp;
printf("%d %d\n", (*first)->num, (*second)->num);
temp = (*first)->num;
(*first)->num = (*second)->num;
(*second)->num = temp;
}

Segmentation fault in C while implementing deque

I am trying to implement a deque in C. I am learning C, so the error might seem very trivial. Here's the entire program
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *prev;
struct node *next;
}Node;
Node *getNode(int data){
Node *temp = (Node *)malloc(sizeof(Node));
temp -> next = temp -> prev = NULL;
temp -> data = data;
return temp;
}
void push_right(Node **head, Node **tail, int data){
Node *newNode = getNode(data);
Node *temp = (*head);
if (temp == NULL){
*head = newNode;
}
else{
while(temp -> next != NULL){
temp = temp -> next;
}
temp -> next = newNode;
newNode -> prev = temp;
*tail = newNode;
}
}
void push_left(Node **head, Node **tail, int data){
Node *newNode = getNode(data);
Node *temp = (*head);
if (temp == NULL){
*head = newNode;
}
else{
newNode -> next = temp;
newNode -> prev = NULL;
(*head) = newNode;
}
}
void remove_right(Node **head, Node **tail, int *val){
Node *temp = (*tail);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else{
*val = temp -> data;
temp = temp -> prev;
(*tail) = temp;
}
free(temp);
}
void remove_left(Node **head, Node **tail, int *val){
Node *temp = (*head);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else{
*val = temp -> data;
temp = temp -> next;
(*tail) = temp;
}
free(temp);
}
void print_all(Node *head){
Node *temp = head;
printf("\n");
while(temp != NULL){
printf("%d\n",temp->data);
temp = temp -> next;
}
}
int main(int argc, char const *argv[])
{
int *val = NULL;
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i){
push_right(&head, &tail,i);
push_left(&head, &tail,i);
}
remove_left(&head, &tail, val);
print_all(head);
return 0;
}
The problem seems to arise when remove_left() is called. I have spend a significant amount of time to understand where the problem is coming from but nothing seems to work. Please help.
This is the output on the Terminal screen.
lib2s-iMac:queue admin$ ./a.out
Segmentation fault: 11
Problem is here :
*val = temp -> data;
Val is NULL, so trying to dereference it will result in segmentation fault.
If you change val type to an int instead of a pointer to an int. And then call remove_left like this :
int main(int argc, char const *argv[])
{ int val = 0;
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i){
push_right(&head, &tail,i);
push_left(&head, &tail,i);
}
remove_left(&head, &tail, &val);
print_all(head);
return 0;
}
This should work.
In your program have 3 main mistakes .
At int *val = NULL; Here it should be int val not int *val.
At function remove_left() . Here You should edit (*head) not (*tail).Also pointer (next,perv) are not managed properly.
Same in the case of remove_right(). Mistakes in managing pointers.
Modified Functions :-
void remove_right(Node **head, Node **tail, int *val) // modified This function.
{
Node *temp = (*tail);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else
{
*val = temp->data;
temp->prev->next = NULL;
(*tail) = temp->prev;
}
free(temp);
}
void remove_left(Node **head, Node **tail, int *val) // modified This function.
{
Node *temp = (*head);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else
{
*val = temp->data;
temp->next->prev = NULL;
(*head) = temp->next;
}
free(temp);
}
Modified main() :-
int main(int argc, char const *argv[])
{
int val = -1; // Modified here
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i)
{
push_right(&head, &tail, i);
push_left(&head, &tail, i);
}
remove_left(&head, &tail, &val);
print_all(head);
return 0;
}
Complete code Online.
Output :-
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
6
7
8
9

Sort a Linked list using C

I am trying to sort a Linked list, but not able to do it. Below is my code. Can anyone help me. I have seen some programs too, which sort linked list and their approach is also like this only.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int push(struct node **h, int x)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
temp->next = *h;
*h = temp;
return 0;
}
void print(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void sort(struct node **h)
{
int i,j,a;
struct node *temp1;
struct node *temp2;
for(temp1=*h;temp1!=NULL;temp1=temp1->next)
{
for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
{
a = temp1->data;
temp1->data = temp2->data;
temp2->data = a;
}
}
}
int main()
{
struct node * head = NULL;
push(&head,5);
push(&head,4);
push(&head,6);
push(&head,2);
push(&head,9);
printf("List is : ");
print(head);
sort(&head);
printf("after sorting list is : ");
print(head);
return 0;
}
Below is the output which i am getting :
List is : 9 2 6 4 5
after sorting list is : 5 4 6 2 9
You're switching the elements no matter what. Compare them first and then swap them if temp2 is less than temp1:
void sort(struct node **h)
{
int i,j,a;
struct node *temp1;
struct node *temp2;
for(temp1=*h;temp1!=NULL;temp1=temp1->next)
{
for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
{
if(temp2->data < temp1->data)
{
a = temp1->data;
temp1->data = temp2->data;
temp2->data = a;
}
}
}
}
In your bubble sort, you forget the swap condition.
In my opinion, I suggest insertion sort
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int push(struct node **h, int x)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
if (*h == NULL) {
temp->next = *h;
*h = temp;
} else {
struct node *tmp = *h;
struct node *prev = NULL;
while (1) {
if (tmp == NULL || tmp->data >= temp->data)
break;
prev = tmp;
tmp = tmp->next;
}
temp->next = tmp;
if (prev != NULL)
prev->next = temp;
else
*h = temp;
}
return 0;
}
void print(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct node * head = NULL;
push(&head,5);
push(&head,4);
push(&head,6);
push(&head,2);
push(&head,9);
printf("List is : ");
print(head);
//sort(&head);
printf("after sorting list is : ");
print(head);
return 0;
}

Error while finding max and min in a circular linked list

I need to find the maximum number and the minimum number in a circular linked list and i should move the minimum number to the start of the list (before head) and the maximum number to the end of the list (before minimum)
why my code is giving me error in output?
Note: Using doubly linked list is disallowed ,we should do this only with circular linked list.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int num;
struct node *next;
} NODE;
void init(NODE **h)
{
(*h) = (NODE*)malloc(sizeof(NODE));
(*h)->next = *h;
}
NODE* add(NODE* h,NODE* p,int x)
{
int i;
NODE *temp;
temp = (NODE*)malloc(sizeof(NODE));
temp->num = x;
temp->next = h;
if(h->next == h)
{
h->next = temp;
return h;
}
else
{
temp = p->next;
p = temp;
return h;
}
return h;
}
NODE* fin(NODE *h,NODE *p)
{
NODE* ptr,*pmin,*pmax,*temp,*temp2,*mnprev,*mxprev;
// temp: minimum
// temp2: maximum
// pmin: holds the minimum
// pmax: holds the maximum
// ptr: o(n) search
// mnprev: holds the previous node of the minimum
// mxprev: hold the previous node of the maximum
mnprev = mxprev = pmin = pmax = h;
ptr = h->next;
int mini, maxi;
mini = h->num;
maxi = h->num;
do
{
if(ptr->num < mini)
{
mini = ptr->num;
pmin = ptr;
mnprev->next = pmin;
}
if(ptr->num > maxi)
{
maxi = ptr->num;
pmax = ptr;
mxprev->next = pmax;
}
ptr = ptr->next;
} while(ptr != h);
temp = pmin;
temp2 = pmax;
mnprev->next = pmin->next;
mxprev->next = pmax->next;
free(pmin);
free(pmax);
temp->next = h;
temp2->next = temp;
ptr = temp;
do {
printf("%d ",ptr->num);
ptr = ptr->next;
} while(ptr != h);
}
int main()
{
int i,x;
NODE *lis,*p;
init(&lis);
p = lis;
for(i=0;i<7;i++)
{
scanf("%d",&x);
add(lis,p,x);
}
fin(lis,p);
}
rewrite your code
function init delete as it will create nodes that are not used.
Changed to the node between the maximum and minimum values ​​since it
is sufficient to simply replace the value.
like this
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *next;
} NODE;
NODE* add(NODE **head, NODE **tail, int x){
NODE *temp;
temp = (NODE*)malloc(sizeof(NODE));
temp->num = x;
if(*head == NULL){
temp->next = temp;
*tail = *head = temp;
} else {
temp->next = *head;
(*tail)->next = temp;
*tail = temp;
}
return *head;
}
void print(NODE *p){
NODE *top =p;
do{
printf("%d ", p->num);
p=p->next;
}while(p != top);
printf("\n");
}
void drop(NODE *head, NODE *tail){
NODE *p = head;
tail->next = NULL;
while(p){
NODE *temp = p;
p = p->next;
free(temp);
}
}
void minmax(NODE *head, NODE *tail){
NODE *p, *maxp, *minp;
int temp;
maxp = minp = p = head;
do{
if(maxp->num < p->num){
maxp = p;
}
if(minp->num > p->num){
minp = p;
}
p = p->next;
}while(p != head);
temp = maxp->num; maxp->num = tail->num; tail->num = temp;
if(tail == minp)//exchanged minp
minp = maxp;//fixed
temp = minp->num; minp->num = head->num; head->num = temp;
}
int main(void){
int i, x;
NODE *head, *tail;
tail = head = NULL;
for(i=0;i<7;i++){
printf("%d>", i+1);
scanf("%d", &x);
add(&head, &tail, x);
}
//print(head);
minmax(head, tail);
print(head);
drop(head, tail);
return 0;
}

Resources