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;
}
Related
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;
}
}
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;
}
I want an array of linked List and obviously each linked should have separate head node. Initially, as an example, I am starting with one array element. I am storing linkedlist into current[0] . But it is giving segmentation fault. If I use Node *current it will create a list and working fine. But, I want to store the list within array. What is wrong with the code ?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *current[20];
void insert_beg_of_list(Node *current[0], int data);
void print_list(Node *current[0]);
void insert_beg_of_list(Node *current[0], int data) {
//keep track of first node
Node *head = current[0];
while(current[0]->next != head) {
current[0] = current[0]->next;
}
current[0]->next = (Node*)malloc(sizeof(Node));
current[0] = current[0]->next;
current[0]->data = data;
current[0]->next = head;
}
void print_list(Node *current[0]) {
Node *head = current[0];
current[0] = current[0]->next;
while(current[0] != head){
printf(" %d ", current[0]->data);
current[0] = current[0]->next;
}
}
int main() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = head;
int data = 0 ;
int usr_input = 0;
int i;
int m;
int j;
scanf("%d", &usr_input);
for (i=0; i<usr_input; i++) {
scanf("%d", &data);
insert_beg_of_list(head, data);
}
printf("The list is ");
print_list(head);
printf("\n\n");
return 0;
}
I think you have mixed the global array current's use. Change your code to this :
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert_beg_of_list(Node *current, int data);
void print_list(Node *current);
void insert_beg_of_list(Node *current, int data) {
//keep track of first node
Node *head = current;
while(current->next != head) {
current = current[0]->next;
}
current->next = malloc(sizeof(Node));
if (current->next == NULL)
return;
current = current->next;
current->data = data;
current->next = head;
}
void print_list(Node *current) {
Node *head = current;
current = current->next;
while(current != head){
printf(" %d ", current->data);
current = current->next;
}
}
int main() {
Node *current[20];
Node *head = malloc(sizeof(Node));
if (head == NULL)
return;
head->next = head;
int data = 0 ;
int usr_input = 0;
int i;
int m;
int j;
scanf("%d", &usr_input);
for (i = 0; i < usr_input; i++) {
scanf("%d", &data);
insert_beg_of_list(head, data);
}
//assign the newly created pointer to a place in the array
current[0] = head;
printf("The list is ");
print_list(head);
printf("\n\n");
return 0;
}
Keep in mind that the parameter current in your functions' prototypes and declarations is not the same as the array current created in your main function. I just left the name as it was.
NOTE : You should do something with head->next pointer, initialize it.
Also read this link on why not to cast the result of malloc and another one on why you should check its result.
You probably want this:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert_beg_of_list(Node *current, int data);
void print_list(Node *current);
void insert_beg_of_list(Node *current, int data) {
//keep track of first node
Node *head = current;
while (current->next != head) {
current = current->next;
}
current->next = (Node*)malloc(sizeof(Node));
current = current->next;
current->data = data;
current->next = head;
}
void print_list(Node *current) {
Node *head = current;
current = current->next;
while (current != head) {
printf(" %d ", current->data);
current = current->next;
}
}
Node *NewList()
{
Node *newnode = (Node *)malloc(sizeof(Node));
newnode->next = newnode;
}
int main() {
Node *arrayofheads[20];
// We are using only arrayofheads[0] in this example
arrayofheads[0] = NewList();
int data = 0;
int usr_input = 0;
int i;
scanf("%d", &usr_input);
for (i = 0; i<usr_input; i++) {
scanf("%d", &data);
insert_beg_of_list(arrayofheads[0], data);
}
printf("The list is ");
print_list(arrayofheads[0]); printf("\n\n");
return 0;
}
I am trying to create a simple linked list using C, I think I was able to construct the linked list itself, but when I try and print it, it prints the value of the last node instead of all the values in the list.
#include <stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
temp->next = NULL;
printf("%d \n", temp->d);
return 0;
}
Any help/tips would be greatly appreciated.
There are multiple problems in your code:
You are not creating the list properly. You are allocating memory for only one node and playing around with pointers improperly causing the linked list to be pointed to the same memory
You are not calling printList function at all
It is not clear why you are allocating memory again in printList whereas it should be printing the already created list
Also, no need to pass double pointer for printList as you are not modifying the list.
Though you should be understanding and doing the changes yourself, I am providing a below modified program which I hope will help you understand the mistakes in your code.
Please see the below modified version of the code
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
struct Node *createNode()
{
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
if (NULL == newNode)
return NULL;
memset(newNode, 0, sizeof(struct Node));
return newNode;
}
int main()
{
struct Node *newNode = NULL;
struct Node *headNode = NULL;
struct Node *temp = NULL;
int i = 0;
int data = 0;
printf("Enter 3 numbers\n");
for( i = 0; i< 3; i++)
{
scanf("%d", &data);
newNode = createNode();
if (NULL == newNode)
break;
newNode->d = data;
if (headNode == NULL)
{
headNode = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = temp->next;
}
}
printList(headNode);
return 0;
}
Replace your code with the following code :-
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp->next!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
printList(head);
return 0;
}
Here you need to declare the newNode in the loop also while taking the input. As, the current value is over-writted, the old value is lossed and only the value of last node is printed.
Also while printing, check for temp->next!=Null instead of temp!=NULL
I found this on Internet to reverse a list using recursion and applied it in codeblocks but the output only reverse prints last two Insert call from main function. It skips the first three Insert calls. Why? I did search for this problem here but I failed to understand them as I'm a beginner. Kindly help
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
struct Node* Insert (struct Node* head, int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
struct Node* head = NULL;
head = Insert(head,2);
head = Insert(head,7);
head = Insert(head,3);
head = Insert(head,1);
head = Insert(head,4);
reversePrint(head);
return 0;
}
O/P : 4 1
NOTES:
Don't cast the return of value of malloc
You declared two *head and confused yourself
No need to pass pointer to function and return pointer when you have head declared as global. Which is not a good idea but I followed your code.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
void Insert (int data)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
Insert(2);
Insert(7);
Insert(3);
Insert(1);
Insert(4);
reversePrint(head);
return 0;
}
OUTPUT:
4 1 3 7 2