basic questions about Linkedlist and Node Insertion - c

I have some problems about my sub-major lecture.
First of all, sorry for poor English.
anyway, Professor told me that It was very east to solve, just change some lines and it would be work.
but I can't finish this code ontime.
when I play? debug? my code, couldn't print 'List'.
How to print properly about my LinkedList code?
+ I have to fix few lines. not entire code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ListNode {
int data;
struct ListNode* link;
}listNode;
void insertFirstListNode(listNode* num, int data) {
listNode* newNode = malloc(sizeof(listNode));
newNode->link = num->link;
newNode->data = data;
num->link = newNode;
}
typedef struct {
listNode* head;
} linkedList_h;
linkedList_h* createLinkedList_h() {
linkedList_h* Newlist = (linkedList_h*)malloc(sizeof(linkedList_h));
Newlist->head = NULL;
return Newlist;
}
void printList(linkedList_h* L) {
listNode* p;
printf("L = (");
p = L->head;
while (p != NULL) {
printf("%d", p->data);
p = p->link;
if (p != NULL) printf(", ");
}
printf(") \n");
}
void main() {
linkedList_h* m;
m = createLinkedList_h();
insertFirstListNode(m, 10);
printList(m);
}

As per my understanding you are trying to add the node to the beginning of the linked list. In that case the below fix should be working fine.
Please have error handling in all the functions like NULL inputs, Malloc fails etc ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ListNode {
int data;
struct ListNode* link;
}listNode;
typedef struct {
listNode* head;
} linkedList_h;
void insertFirstListNode(linkedList_h* num, int data) {
listNode* newNode = (listNode *)malloc(sizeof(listNode));
newNode->link = num->head;
newNode->data = data;
num->head = newNode;
}
linkedList_h* createLinkedList_h() {
linkedList_h* Newlist = (linkedList_h*)malloc(sizeof(linkedList_h));
Newlist->head = NULL;
return Newlist;
}
void printList(linkedList_h* L) {
listNode* p;
printf("L = (");
p = L->head;
while (p != NULL) {
printf("%d", p->data);
p = p->link;
if (p != NULL) printf(", ");
}
printf(") \n");
}
int main() {
linkedList_h* m;
m = createLinkedList_h();
insertFirstListNode(m, 10);
printList(m);
return 0;
}

Related

How can I delete the front node of my doubly linked list?

I want to delete the front node in delete_first.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct DListNode {
element data;
struct DListNode *llink;
struct DListNode *rlink;
} DlistNode;
void init(DListNode *phead) {
phead->llink = phead;
phead->rlink = phead;
}
An error occurs in the part below when I debug.
void print_dlist(DListNode *phead) {
DListNode *p;
for (p = phead->rlink; p != phead; p = p->rlink) {
printf("<-| |%d| |->", p->data);
}
printf("\n");
}
This is my insert code and I think it is not problem
void dinsert(DListNode *before, element data) {
DListNode *newnode = (DListNode *)malloc(sizeof(DListNode));
newnode->data = data;
newnode->llink = before;
newnode->rlink = before->rlink;
before->rlink->llink = newnode;
before->rlink = newnode;
}
I want to fix some error in that delete_first code but I don't know what is the problem and how to fix it.
void delete_first(DListNode *head) {
head = head->rlink;
if (head != NULL) {
head->llink = NULL;
}
free(head);
}
int main() {
DListNode *head = (DListNode *)malloc(sizeof(DListNode));
init(head);
printf("insert\n");
for (int i = 0; i < 5; i++) {
dinsert(head, i);
print_dlist(head);
}
printf("\n delete \n");
delete_first(head);
print_dlist(head);
free(head);
return 0;
}
How can I delete my front node using the head node?
To delete the first node of the doubly linked list, you must free the node pointed to by head->rlink unless head->rlink points to head itself, which is the case if the list is empty:
void delete_first(DListNode *head) {
DListNode *p = head->rlink;
if (p != head) {
p->llink->rlink = p->rlink;
p->rlink->llink = p->llink;
free(head);
}
}

Garbage value for certain C functions

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...

Keep getting a Segmentation fault: 11

#include <stdio.h>
#include <stdlib.h>
// A Tree node
typedef struct leaf
{
char ch;
int freq;
struct leaf *left, *right;
}leaf;
typedef struct node{
int data;
leaf* leaf_data;
struct node *ptr;
} node;
//----- LINKED LIST -----
struct LinkedList {
node* head;
//node* tail;
};
typedef struct LinkedList LinkedList_s;
leaf* createLeaf(char ch, int freq)
{
leaf* node = malloc(sizeof(leaf));
node->ch = ch;
node->freq = freq;
node->left = NULL;
node->right= NULL;
return node;
}
//----- CREATE LINKED LIST -----
LinkedList_s createSortedList()
{
LinkedList_s list = {NULL};
return list;
}
node* createStackNode(leaf* l){
node* temp = (node*) malloc(sizeof(node)) ;
temp->data = (int) l->freq;
temp->leaf_data = l;
return temp;
}
void insert(LinkedList_s* head, node* l) {
if(head->head == NULL){
head->head = l;
}
else if (head->head->data > l->data){
l->ptr = head->head;
head->head = l;
}
else{
node *prev = (node*)malloc(sizeof(node));
node *next = (node*)malloc(sizeof(node));
int i = 0;
prev = NULL;
next = head->head;
while(next != NULL && i == 0){
if (next->data >= l->data){
prev->ptr = l;
l->ptr = next;
i = 1;
}
prev = next;
next = next->ptr;
}
if (next == NULL){
prev->ptr = l;
l->ptr = next;
}
}
}
node* dequeue(LinkedList_s *list){
node* temp = (node*)malloc(sizeof(node));
temp = list->head;
list->head = list->head->ptr;
return temp;
}
void insertLeaf(LinkedList_s *list, leaf* l){
node* n = createStackNode(l);
//printf("%d", n->data);
insert(list, n);
}
basically I'm building a huffman encoder for a class project and I'm trying to create a sorted list/priority queue to store BTS leaf node in order of frequency. I'm having problems with createStackNode. If I change the node struct int data to int* data I don't get a seg. fault, but this way it randomly seg. faults and won't sort. Any ideas would be much appreciated!
So I call insertLeft from a separate file
#include <stdio.h>
#include <stdlib.h>
#include "Sorted_List.h"
#include "linked_counter.h"
int main(void){
//Make a Frequency List
LinkedList_t* x = malloc(sizeof(LinkedList_t));
x = makeList();
//Create a Sorted List/Priority Queue
struct LinkedList_s* list = malloc(sizeof(LinkedList_s));
*list = createSortedList();
leaf* l = malloc(sizeof(leaf));
//Current node for traversal
Node_t* current = malloc(sizeof(Node_t));
current= x->head;
while(current != NULL){
l = createLeaf(current->data, current->count);
printf("%c %d ->",l->ch, l->freq);
insertLeaf(list, l);
current= current->next;
}
//for(int i = 0; i < 5; i++)
//printf("%c~~\n", list->head->leaf_data->ch);
}
at insert leaf it gives the fault

Linked List using recursion - Unexpected Output

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

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;
}

Resources