I'm writing a program that creates a doubly linked list and removes a certain element from it. Everything pretty much works, except for the part when the list consists only of 1 element and when I try to delete it, program crashes. Any suggestions?
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head;
struct Node* tail;
struct Node* CreateNewNode(int x){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
void InsertAtBegin(int x){
struct Node* newNode=CreateNewNode(x);
if(head == NULL) {
head = newNode;
tail = newNode;
return;
}
head->prev = newNode;
newNode->next = head;
head = newNode;
}
void InsertAtEnd(int x){
struct Node* newNode=CreateNewNode(x);
if(tail==NULL){
head=newNode;
tail=newNode;
return;
}
tail->next=newNode;
newNode->prev=tail;
tail=newNode;
}
struct Node* PointTo(int k){
struct Node* curr=head;
int i=1;
while(curr!=NULL && i<k){
curr=curr->next;
i++;
}
return curr;
}
void DelFromList(int k){
struct Node* temp;
temp=PointTo(k);
if(k==1){
head=temp->next;
temp->next->prev=NULL;
free(temp);
return;
}
if(temp->next==NULL){
temp->prev->next=NULL;
tail=temp->prev;
free(temp);
return;
}
if(temp->next==NULL && temp->prev==NULL){
head=NULL;
tail=NULL;
printf("atpazista\n");
free(temp);
return;
}
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
free(temp);
}
void Print(){
struct Node* temp = head;
if(head==NULL){
printf("List empty\n");
return;
}
printf("List: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void Read(int *x){
while(scanf("%d", x)==0){
printf("NUMBERS ONLY\n");
scanf("%s");
}
}
void Free(){
struct Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
free(temp->prev);
}
}
int main()
{
int n=0, x;
printf("Number of elements?\n");
while(n<=0){
Read(&n);
}
int i;
for(i=0; i<n; i++){
printf("Type a number\n");
Read(&x);
InsertAtEnd(x);
}
Print();
printf("Head: %d\n", head->data);
printf("Tail: %d\n", tail->data);
printf("Number of element to be deleted?\n");
n=0;
while(n<=0){
Read(&n);
}
DelFromList(n);
Print();
printf("Head: %d\n", head->data);
printf("Tail: %d\n", tail->data);
Free();
return 0;
}
Perhaps swap the ordering of:
if(temp->next==NULL){
temp->prev->next=NULL;
tail=temp->prev;
free(temp);
return;
}
if(temp->next==NULL && temp->prev==NULL){
head=NULL;
tail=NULL;
printf("atpazista\n");
free(temp);
return;
}
The latter code will never execute because the first case already did, and returned
Related
After I enter numbers in the terminal, the program gets stuck at the free(temp) command line as shown.
https://i.stack.imgur.com/BycHf.png
The program does not work properly until the "Continue" button is clicked twice.
https://i.stack.imgur.com/RDO2b.png
https://i.stack.imgur.com/7gqzN.png
The source code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head;
void Insert(int n){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = n;
temp->next = head;
head = temp;
}
void Delete(int z){
struct Node* temp = head;
if(z == 1){
head = temp->next;
free(temp);
return;
}
int i;
for(i=0;i<z-2;i++){
temp = temp->next;
}
struct Node* temp1;
temp1 = temp->next;
temp->next = temp1->next;
free(temp);
}
void print(){
printf("List is:");
int x;
struct Node* temp1;
temp1 = head;
while(temp1 != NULL){
x = temp1->data;
printf("%d ",x);
temp1 = temp1->next;
}
printf("\n");
}
int main()
{
head = NULL;
int k;
Insert(2);
Insert(5);
Insert(4);
Insert(7);
Insert(0);
Insert(8);
Insert(5);
Insert(6);
print();
printf("you want to delete:");
scanf("%d",&k);
Delete(k);
print();
system("pause");
return 0;
}
So I'm very new to programming, so I would like you to keep in mind theres a high chance I've made a very dumb or basic mistake. I have run into this problem while trying to create a linked list in C. For the output, I'm able to enter 2 elements before I get a segmentation fault:11.
#include<stdio.h>
struct node {
int data;
struct node *next;
};
void create(){
int temp1,temp2;
printf("Enter the number of elements\n");
scanf("%d",&temp1);
struct node *x=(struct node*)malloc(temp1*sizeof(struct node*));
for(int i=0;i<temp1;i++){
printf("loop\n");
printf("Enter a value\n");
scanf("%d",&temp2);
x->data=temp2;
printf("text\n");
x=x->next;
}
x->next=NULL;
}
int main(){
create();
}
x=x->next;
}
x->next=NULL;
you have not allocated any memory for the next and then you dereference it.
BTW you do not save anywhere the fist node so after the function call the list and allocated memory is lost
Hi I modified your code a bit. Below is the modified version
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void print(struct node *head) {
while(head){
printf("%d->", head->data);
head = head->next;
}
printf("\n");
}
void free_mem(struct node *head) {
struct node *temp;
while(head){
temp = head;
head = head->next;
free(temp);
}
}
//Insertion at the end
struct node *create(struct node *head){
int temp1,temp2;
struct node *temp_node;
printf("Enter the number of elements\n");
if(scanf("%d",&temp1) < 1){
printf("scanf for temp1 failed!\n");
exit(1);
}
for(int i=0;i<temp1;i++){
struct node *x;
if(! (x = (struct node*)malloc(sizeof(struct node)))){
printf("malloc of new node failed!\n");
exit(1);
}
printf("Enter a value\n");
if(scanf("%d",&temp2) < 1){
printf("scanf for temp2 failed!\n");
exit(1);
}
x->data=temp2;
x->next = NULL;
if(!head){
head = x;
head->next = NULL;
continue;
}
//Moving to end
temp_node = head;
while(temp_node->next){
temp_node = temp_node->next;
}
temp_node->next = x;
}
return head;
}
int main() {
struct node *head = NULL;
head = create(head);
print(head);
//freeing dynamically allocated memory
free_mem(head);
return 0;
}
Please revert for any clarifications.
I'm working on a C code. I have a singly linked list, and i want to delete a node,which i don't know the position of it. My struct is this:
struct list_node{
int num;
char name[25];
int year;
float money;
struct list_node *next;
};
typedef struct list_node node;
node *head=NULL;
I have a value which i have to compare with the node->name so it deletes the node. How can i do this? I had tried with this code to do this:
value=argv[2];
temp=head;
if(temp==NULL){
return 0;
}
if(strcmp(current->name,value)==0){
temp=head;
head=head->next;
free(temp);
return 0;
}
p=head;
while(p->next!=NULL){
if(strcmp(p->next->name,value)==0){
temp=p->next;
p->next=temp->next;
free(temp);
return 0;
}
p=p->next;
}
return 0;
}
but the node was not deleted. (I have a function to print the list)
Try this code.. I think you probably have made mistake in taking pointers to structures!
{
node* temp;
node * ptr=head;
if(ptr=NULL)
{
return 0;
}
else if(strcmp(ptr->name, name2)==0)
{
head=head->next;
free(ptr);
return 0;
}
while(ptr->next!=NULL)
{
if(strcmp(ptr->next->name, name2)==0)
{
temp=ptr->next;
ptr->next=ptr->next->next;
free(temp);
return 0;
}
ptr=ptr->next;
}
printf("Node not found!");
return 0;
}
I have a value which i have to compare with the node->name so it
deletes the node. How can i do this?
… not too complicated:
node *curr, **prev;
for (prev = &head; curr = *prev; prev = &curr->next)
if (strcmp(curr->name, value) == 0)
{
*prev = curr->next,
free(curr);
break;
}
hello , to day i'm want to change Insert At Front linked list to Insert At Back linked list,i'm not sure Insert At Back code but Insert At Front code not have bug .
problem list is function void insert_at_back(LN **hptr, int d) in code
second.
how to from : C program to insert node at the end of Singly Linked List
this is Insert At Front linked list code (NOT have bug)
#include<stdio.h>
#include<stdlib.h>
struct listnode {
int data;
struct listnode *next;
};
typedef struct listnode LN;
void insert_at_front(LN **hptr, int d);
void print(LN *head);
int sum(LN *head);
int main(){
LN *head;
head = NULL;
int d;
printf("Enter data: ");
do{
scanf("%d", &d);
if(d > 0){
insert_at_front(&head,d);
}
}while(d > 0);
printf("=");
print(head);
printf("\n=%d", sum(head));
return 0;
}
void insert_at_front(LN **hptr, int d){
LN* newNode= (LN*)malloc(sizeof(LN));
newNode->data = d;
newNode->next= *hptr;
*hptr = newNode;
}
void print(LN *head){
while(head !=NULL){
printf("%d ",head->data);
head = head->next;
}
}
int sum(LN *head){
int temp=0;
while(head !=NULL){
temp+=head->data;
head = head->next;
}
return temp;
}
This is Insert At Back linked list code (it have problem or bug)
#include<stdio.h>
#include<stdlib.h>
struct listnode {
int data;
struct listnode *next;
};
typedef struct listnode LN;
void insert_at_back(LN **hptr, int d);
void print(LN *head);
int sum(LN *head);
int main(){
LN *head;
head = NULL;
int d;
printf("Enter data: ");
do{
scanf("%d", &d);
if(d > 0){
insert_at_back(&head,d);
}
}while(d > 0);
printf("=");
print(head);
printf("\n=%d", sum(head));
return 0;
}
void insert_at_back(LN **hptr, int d){
LN* head = *hptr;
LN* newNode = (LN*)malloc(sizeof(LN));
if(newNode==NULL){
printf("Unable to allocate memory\n");
}else{
newNode->data = d;
newNode->next= NULL;
while(head->data!= NULL){
head=head->next;
}
head->next=newNode;
}
}
void print(LN *head){
while(head !=NULL){
printf("%d ",head->data);
head = head->next;
}
}
int sum(LN *head){
int temp=0;
while(head !=NULL){
temp+=head->data;
head = head->next;
}
return temp;
}
Thank you for answer ,i'm sorry if u not understand.
i'm newbie
ihm
There are few of problems in your insert_at_back() function.
First problem:
while(head->data!= NULL){
This should be :
while(head->next!= NULL){
Second problem:
Not checking for head pointer NULL in insert_at_back() function.
Third problem:
Not initializing the hptr in insert_at_back() function.
Your insert_at_back() should be:
void insert_at_back(LN **hptr, int d){
LN* head = *hptr;
LN* newNode = malloc(sizeof(LN));
if(newNode==NULL){
printf("Unable to allocate memory\n");
}else{
newNode->data = d;
newNode->next= NULL;
if (head != NULL) {
while(head->next!= NULL){
head=head->next;
}
head->next=newNode;
}
else
*hptr = newNode;
}
}
Last but not least - do not typecast malloc result.
I am having some trouble adding integers to the end of my linked list. I am very new to C and had part of my program working properly (the push function). I want to return a pointer to a struct node, and I am not quite sure where I am going wrong in my append function.
~Thanks.
enter code here
//node.h
#ifndef NODE_H
#define NODE_H
struct node{
int val;
struct node *next;
};
int length(struct node *);
struct node* push(struct node *, int); //adds integer to front of list.
struct node* append(struct node *, int); //adds integer to back of list.
void print(struct node *, int);
#endif
//node.c
#include "./node.h"
#include<stdlib.h>
#include<stdio.h>
int length(struct node *current){
if(current->next != NULL)
return 1 + length(current->next);
else
return 1;
}
struct node* push(struct node *head, int num){
struct node *temp = malloc(sizeof(struct node));
temp->val = num;
temp->next = head;
head = temp;
temp = NULL;
return head;
}
struct node* append(struct node *current, int num){
if(current != NULL){
append(current->next, num);
}
else{
struct node* temp = malloc(sizeof(struct node));
temp->val = num;
temp->next = NULL;
current = temp;
return current;
}
}
void print(struct node* head, int size){
printf("The list is %i", size);
printf(" long \n");
struct node* temp;
temp = head;
while(temp != NULL){
printf("%d", temp->val);
printf(" ");
temp = temp->next;
}
printf(" \n");
}
//Main
#include "./node.h"
#include<stdlib.h>
#include<stdio.h>
int main(){
char ans[2];
int num;
struct node* head = NULL;
do{
printf("Enter a integer for linked list: ");
scanf("%d", &num);
head = append(head, num);
printf("Add another integer to linked list? (y or n) ");
scanf("%1s", ans);
}while(*ans == 'y');
print(head, length(head));
return 0;
}
I think what is missing is that the recursive part of the function needs to set current->next. This has the effect of setting every node's next pointer to what it was until you get to the end of the list, when it is set to the newly malloced node.
struct node* append(struct node *current, int num){
if(current != NULL){
current->next = append(current->next, num);
return current;
}
else {
struct node* temp = malloc(sizeof(struct node));
if (temp == NULL) abort();
temp->val = num;
temp->next = NULL;
return temp;
}
}