I have a very basic doubt. I created a linklist using structure and this is my delete code.
void delete(int num)
{
struct node* temp=head;
struct node* prev=head;
if(temp == NULL)
printf("List Empty\n");
else
{
while(temp != NULL)
{
if(temp->value == num)
{
prev=temp->next;
free(temp);
break;
}
else
{
prev=temp;
temp=temp->next;
}
}
After running this code, the node is not getting deleted. If I print temp->value after free(temp), the value is 0. But this should not be the case. The free should wipe the node. So I don't understand from where the 0 appears. Any idea what is wrong with this code ?
My show function:
void show()
{
struct node *temp = head;
while(temp != NULL)
{
printf("---- %d ---- ", temp->value);
temp=temp->next;
}
printf("\n\n");
}
My struct:
struct node
{
int value;
int pos;
struct node* next;
};
Thanks.
When you find the node in delete:
if(temp->value == num)
{
prev=temp->next;
free(temp);
break;
}
You don't actually make the previous nodes next-pointer point to the nodes next link.
Instead you should do e.g.
prev->next = temp->next;
Related
I tried to implement a binary tree by using an iterative function to create it. I'm quite confused about why I'm getting an infinite loop as my output. I believe that the function being used is not having problems. But if anyone can explain me what exactly is causing this error I would appreciate it.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *left,*right;
} NODE;
NODE* createNode(int ele){
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = ele;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
void inorder(NODE* root){
while(root != NULL){
inorder(root->left);
printf(" %d ",root->data);
inorder(root->right);
}
}
void preorder(NODE* root){
while(root != NULL){
printf(" %d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(NODE* root){
while(root != NULL){
postorder(root->left);
postorder(root->right);
printf(" %d ",root->data);
}
}
void createTree(NODE** root, int ele){
NODE* newnode = createNode(ele);
if(*root == NULL){
//set newnode as root
*root = newnode;
return;
}
NODE* curr = *root;
while (1){
// If the data of the new node is less than the data of the current node,
// go to the left child
if(ele < curr->data){
// If the left child is empty, insert the new node here
if(curr->left == NULL){
curr->left = newnode;
return;
}
curr = curr->left;
}
// If the data of the new node is greater than or equal to the data of the current node,
// go to the right child
else{
if (curr->right == NULL){
curr->right = newnode;
return;
}
curr = curr->right;
}
}
}
int main(){
NODE* root = NULL;
int data;
while(1){
printf("Enter the data for the root node of the binary tree(Enter -1 to stop): \n");
scanf("%d",&data);
createTree(&root,data);
if(data == -1){
break;
}
}
printf("\nPreorder traversal:\n");
preorder(root);
printf("\nInorder traversal:\n");
inorder(root);
printf("\nPostorder traversal:\n");
postorder(root);
return 0;
}
In the print functions, the line
while(root != NULL){
will cause endless looping as root never change.
You probably want
if(root != NULL){
I have looked at my code several times but couldn't find the problem. please tell me what I need to replace to get my code working.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void insert(int x)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data = x;
temp->next = NULL;
if (head == NULL)
{
temp->next = head;
head = temp;
return;
}
struct node *temp1 = head;
while(temp1 != NULL)
{
temp1 = temp1->next;
}
temp1->next = temp;
}
void display()
{
struct node *temp = head;
if (head == NULL)
{
printf("list is empty");
return;
}
else{
while(temp!=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}
int main()
{
head = NULL;
insert(3);
insert(5);
insert(6);
display();
}
The problem is in this code:
struct node *temp1=head;
while(temp1!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
... the while loop won't end until temp1 is NULL, so after the loop ends, it is guaranteed that temp1 is a NULL pointer ... and then you dereference that NULL pointer (via temp1->next), which causes a crash. Probably what you want to do instead is while(temp1->next != NULL) {...}
while(temp1!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
The only way out of this loop is for temp1 to be NULL. Then the next line attempts to used temp1 as a pointer. This is likely causing your issue. You need to instead check if the next is NULL and break leaving temp1 as the last in the list not it's next.
Pro tip for linked lists like this, they are a lot easier to modify with double pointers. Example code:
void append(struct node **list, int a) {
// skip to the end of the list:
while (*list != NULL) {
list = &(*list)->next;
}
*list = malloc(sizeof(struct node));
(*list)->data = a;
(*list)->next = NULL;
}
void display(struct node *list) {
while (list) {
printf("%d\n", list->data);
list = list->next;
}
}
void remove(struct node **list, int index) {
while (*list) {
if (--index == 0) {
struct node *temp = *list;
*list = temp->next;
free(temp);
break;
}
}
}
int main() {
struct list *mylist;
append(&mylist, 3);
append(&mylist, 4);
append(&mylist, 5);
display(mylist); // prints 3 4 5
remove(&mylist, 1);
display(mylist); // prints 3 5
remove(&mylist, 0);
remove(&mylist, 0);
// mylist is NULL again, all memory free'd
}
Note that this code needs no special cases for "is the list empty?", which makes it less complex than yours.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I just made a program of double linked list in which I was trying to print the values after every insertion operation done.
After first insertion no value is being printed but from the second insertion the value is printing fine(except the first one).
I am hereby attaching the full code
// Double Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next,*prev;
};
struct node *head;
struct node *getnewnode(int);
void insertathead(int);
void insertattail(int);
void display();
void rev_display();
void main()
{
char c;
int n,n1;
clrscr();
head = NULL;
do
{
printf("\n Enter Data Element");
scanf("%d", &n);
printf("Press 1 to insert at beginning \n Press 2 to insert at the end");
scanf("%d", &n1);
if(n1 == 1)
{
insertathead(n);
display();
rev_display();
}
if(n1 == 2)
{
insertattail(n);
display();
rev_display();
}
printf("Do you wish to enter more (Y/N)");
c = getch();
} while(c == 'Y' || c == 'y');
getch();
}
struct node *getnewnode(int x)
{
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return(newnode);
}
void insertathead(int x)
{
struct node *temp = getnewnode(x);
if(head == NULL)
{
head = temp;
}
else
{
head->prev = temp;
temp->next = head;
head = temp;
}
}
void display()
{
struct node *temp;
temp = head;
printf("Forward:\n");
while(temp->next != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void rev_display()
{
struct node *temp;
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
while(temp->prev != NULL)
{
printf("%d ", temp->data);
temp = temp->prev;
}
}
void insertattail(int x)
{
struct node *temp = getnewnode(x);
struct node *t;
t = head;
while(t->next != NULL)
{
t = t->next;
}
t->next = temp;
temp->prev = t;
}
The error is in the definition of the while loop. You stop when you reach a setting with no previous entry. You should stop when the current entry is NULL
Also note that in your original you call rev_display() but you defined the function as rev_dispaly(). That typo should be fixed.
You also assume that insertattail() never has a case in which the list is empty (head == NULL) I will show how that occurs after the rev_display fix.
void rev_display()
{
struct node *temp;
temp=head;
// This correctly finds the last entry
while(temp->next!=NULL)
{
temp=temp->next;
}
/* This will stop when you reach the entry with no previous entry */
while(temp->prev!=NULL)
{
printf("%d ",temp->data);
temp=temp->prev;
}
}
The code should really be
void rev_display()
{
struct node *temp;
temp=head;
// This correctly finds the last entry
while(temp->next!=NULL)
{
temp=temp->next;
}
/* This will correctly include the head as well in the print */
while(temp != NULL)
{
printf("%d ",temp->data);
temp=temp->prev;
}
}
You do not check for the empty list case in insertattail().
void insertattail(int x)
{
struct node *temp=getnewnode(x);
struct node *t;
t=head;
// Note that this assumes that the list is not empty
while(t->next!=NULL)
{
t=t->next;
}
t->next=temp;
temp->prev=t;
}
This needs to check for the empty list.
void insertattail(int x)
{
struct node *temp=getnewnode(x);
struct node *t;
// First check if the list is empty
if(head==NULL)
{
head=temp;
head->next = NULL;
head->prev = NULL;
}
else
{
t=head;
// This list is not empty so find the end
while(t->next!=NULL)
{
t=t->next;
}
t->next=temp;
temp->prev=t;
}
}
I am trying to delete from a Singly Linked List, however, when I try to delete from the first element, it prints garbage. I think the problem comes from the delete_node function, however, I tried everything and I cannot figure it out.
#include <stdio.h>//prinf
#include <stdlib.h>//alloc mallco callo
typedef struct node node;
struct node{
int number;
node *next;
};
node *new_node(int num){
node *n= (node*) malloc(sizeof(node));
n->number=num;
n->next=NULL;
return n;
}
void node_free_all(node *n){
if(n != NULL){
node_free_all(n->next);
free(n);
}
}
void print_nodes(node *n){
if(n != NULL){
print_nodes(n->next);
printf("Number is: %d\n",n->number);
}
}
void delete_node(node *n, int num){
node *rmNode= (node*)malloc(sizeof(node));
//delete first
if( n!= NULL && n->number==num){
rmNode = n;
n=n->next;
free(rmNode);
}
//all but first
while(n != NULL){
if(n->next != NULL && n->next->number == num){
rmNode= n->next;
n->next = rmNode->next;
free(rmNode);
break;
}
n=n->next;
}
}
int main(){
int i;
node *head= (node*) malloc(sizeof(node));
node *curr;
head=NULL;
for(i=1;i<=10;i++) {
curr = new_node(i);
curr->next =head;
head=curr;
}
printf("Everything:\n");
print_nodes(head);
printf("Deleting 1:\n");
delete_node(head,1);
print_nodes(head);
printf("Deleting 5:\n");
delete_node(head,5);
print_nodes(head);
printf("Deleting 2:\n");
delete_node(head,2);
print_nodes(head);
printf("Deleting 3:\n");
delete_node(head,3);
print_nodes(head);
printf("Deleting 10:\n");
delete_node(head,10);
print_nodes(head);
printf("Deleting 9:\n");
delete_node(head,9);
print_nodes(head);
node_free_all(head);
// node_free_all(list);
return 0;
}
What am I doing wrong?
You don't need to allocate memory to rmNode. Plus you need to pass the reference of head pointer to the function delete_node because every time you are updating the list and if you have to delete first element of list, then in this case head pointer also got updated.
struct node
{
int number;
node *next;
};
void delete_node(struct node** head_ref, int num)
{
struct node* temp;
struct node* current = (*head_ref);
//delete first
if( current != NULL && current->number == num)
{
temp = current;
current = current->next;
free(temp);
(*head_ref) = current;
}
else
{
//all but first
while(current != NULL)
{
if(current->next != NULL && current->next->number == num)
{
temp = current->next;
current->next = temp->next;
free(temp);
break;
}
current = current->next;
}
}
}
here's a version that removes all items that matches num, and always update the head item.
void delete_node(node** head_ref, int num)
{
node* temp;
node* last = 0;
node* current = *head_ref;
while(current)
{
if( current->number == num )
{
temp = current;
if( current == *head_ref )
current = (*head_ref) = current->next;
else
current = last->next = current->next;
free(temp);
} else {
last = current;
current = current->next;
}
}
}
You have to call it like delete_node(&head,1);
because it needs an address to the head item to be able to change it
void sortlist()
{
struct node *a;
struct node *temp=head;
struct node *temp1=head->next;
while(temp!=NULL)
{
while(temp1->next!=NULL)
{
if(temp->data > temp1->data)
{
a->data=temp->data;
temp->data=temp1->data;
temp1->data=a->data;
}
else
{
temp1=temp1->next;
}
}
temp=temp->next;
}
}
//I am new to data structures.i am encountering some problem here while trying to sort elements of linked list.list does not get sorted.any help is greatly appreciated.
a is an uninitialised pointer so the line
a->data=temp->data;
invokes undefined behaviour. Crashing is the most likely result here as you'll try to write to memory at an undefined address that may not be writeable by your code or may be in use by another part of your program.
You could fix this by giving a the same type as temp->data instead.
void sortlist()
{
int a; // change type to match node->data if required
...
if(temp->data > temp1->data)
{
a=temp->data;
temp->data=temp1->data;
temp1->data=a
}
...
}
EDIT: There is also a potential NULL dereference crash in the line while(temp1->next!=NULL). The code below shows a potential implementation of sortlist which avoids this.
void sortlist()
{
struct node *ptr=head;
while(ptr!=NULL) {
struct node *next;
if (ptr == NULL)
return;
next = ptr->next;
while(next!=NULL) {
if(ptr->data > next->data) {
int a=ptr->data;
ptr->data=next->data;
next->data=a;
}
next = next->next;
}
ptr=ptr->next;
}
}
I made changes in the code and add the comments for the changes
void sortlist()
{
// struct node *a; /* this is not required. */
/* NULL checks are required,if head is NULL,head->next will crash.*/
if(head == NULL || head->next == NULL) return;
struct node *temp=head;
struct node *temp1=head->next;
while(temp!=NULL)
{
while(temp1->next!=NULL)
{
if(temp->data > temp1->data)
{
/* assuming you data is integer*/
int d=temp->data;
temp->data=temp1->data;
temp1->data=d;
}
//else /* else is not required, better to remove it.*/
//{
temp1=temp1->next;
//}
}
temp=temp->next;
}
}
//at last i found answer to my own problem and this is the solution,thanks for your help buddies
void sortlist()
{
struct node *temp=head;
int s;
struct node *temp1=temp->next;
while(temp!=NULL)
{
temp1=temp->next;
while(temp1!=NULL)
{
if(temp->data > temp1->data)
{
s=temp->data;
temp->data=temp1->data;
temp1->data=s;
}
temp1=temp1->next;
}
temp=temp->next;
}
}