c linked list delete smallest element [closed] - c

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
Lists seem really hard for me. I want to find the smallest element in a file: 1 5 8 6 4 8 6 48 9. It's 1 and I want to delete that 1. I can find the smallest element but can not delete it. I find the smallest element place but not the value. I tried copying deleting function from the web, however I cant understand it due to the fact that I'm really new to C. It writes an error that dereferencing to incomplete type. Please help. Post whole code because it should be more convenient to understand.
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList {
int value;
struct linkedList *next;
} linkedList, head;
linkedList *readList(linkedList *head) {
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if (dataFile == NULL) {
printf("Nepasisekė atidaryti failo\n");
} else {
printf("Duomenų failą pavyko atidaryti\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(linkedList));
fscanf(dataFile, "%d", &head->value);
head->next = NULL;
} else {
struct linkedList *current = head;
struct linkedList *temp = malloc(sizeof(linkedList));
while (current->next != NULL) {
current = current->next;
}
fscanf(dataFile, "%d", &temp->value);
current->next = temp;
temp->next = NULL;
}
return head;
}
void search(linkedList *head, int *lowest) {
int a[100];
int i = 0;
int minimum;
int b = 0;
linkedList *current = head;
while (current != NULL) {
a[i] = current->value;
current = current->next;
i++;
}
b = i;
i = 0;
minimum = a[0];
while (b > 0) {
if (minimum > a[i]) {
minimum = a[i];
lowest = i;
}
i++;
b--;
}
}
void deleteNode(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
void printList(linkedList *head) {
linkedList *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current->next;
}
printf("NULL\n");
return;
}
int main() {
linkedList *A = NULL;
A = readList(A);
search(A);
head = head->next;
minimum = head->value;
headk->next = head->next;
free(head);
printList(A);
return 0;
}

like this
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct linkedList{
int value;
struct linkedList *next;
} linkedList, node;
linkedList *readList(void){
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if(dataFile == NULL) {
perror("file open");
return NULL;
}
int v;
node head = {0, NULL}, *curr = &head;
while (1 == fscanf(dataFile, "%d", &v)){
node *new_node = malloc(sizeof(node));
if(new_node == NULL){
perror("malloc");
break;
}
new_node->value = v;
new_node->next = NULL;
curr = curr->next = new_node;
}
fclose(dataFile);
return head.next;
}
int searchMin(linkedList *head){
if(head == NULL){
fprintf(stderr, "%s: The list MUST NOT be NULL.\n", __func__);
return INT_MIN;
}
int min = head->value;
node *p = head->next;
while(p){
if(p->value < min)
min = p->value;
p = p->next;
}
return min;
}
void deleteNode(node **head_ref, int key){
node *curr = *head_ref, *prev = NULL;
while (curr != NULL && curr->value != key){
prev = curr;
curr = curr->next;
}
if (curr == NULL) return;//not found
if(prev)
prev->next = curr->next;
else
*head_ref = curr->next;
free(curr);
}
void printList(linkedList *head){
node *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current -> next;
}
puts("NULL");
}
void freeList(linkedList *list){
while(list){
node *temp = list;
list = list->next;
free(temp);
}
}
int main(void){
linkedList *A = readList();
int min = searchMin(A);
printList(A);
deleteNode(&A, min);
printList(A);
freeList(A);
return 0;
}

Please try if this program can help you.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node {
int data;
struct node *next;
};
void push(struct node **head_ref, int new_data) {
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct node **head_ref, int key) {
struct node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
void min(struct node **q) {
struct node *r;
int min = INT_MAX;;
r = *q;
while (r != NULL) {
if (r->data < min) {
min = r->data;
}
r = r->next;
}
printf("The min is %d", min);
deleteNode(q, min);
printf("\n");
}
int main() {
struct node *head = NULL;
FILE *file = fopen("duom.txt", "r");
int i = 0;
fscanf(file, "%d", &i);
while (!feof(file)) {
push(&head, i);
fscanf(file, "%d", &i);
}
fclose(file);
puts("Created Linked List: ");
printList(head);
min(&head);
puts("\nLinked List after Deletion of minimum: ");
printList(head);
return 0;
}
file duom.txt
1 5 8 6 4 8 6 48 9
Test
./a.out
Created Linked List:
9 48 6 8 4 6 8 5 1 The min is 1
Linked List after Deletion of minimum:
9 48 6 8 4 6 8 5 ⏎

Related

Trouble compiling .c and header files with gcc in Ubuntu WSL

I am trying to compile some c code with multithreading and for some reason I'm getting a segmentation fault in the Ubuntu WSL terminal when I try to run:
gcc -o mashu concurrent_list.c concurrent_list.h
The files I am trying to run are the following:
concurrent_list.c:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "concurrent_list.h"
struct node {
int value;
node* next;
pthread_mutex_t* lock;
// add more fields
};
struct list {
// add fields
node* head;
pthread_mutex_t* lock;
};
void print_node(node* node)
{
// DO NOT DELETE
if(node)
{
printf("%d ", node->value);
}
}
list* create_list()
{
// add code here
list* l = malloc(sizeof(list));
if(l == NULL){
printf("malloc error");
}
l->head = NULL;
l->head->next = NULL;
if(pthread_mutex_init(l->lock, NULL) != 0){
printf("mutex init failed\n");
}
if(pthread_mutex_init(l->head->lock, NULL) != 0){
printf("mutex init failed\n");
}
return l;
}
void delete_list(list* list)
{
// add code here
pthread_mutex_lock(list->lock);
node* head = list->head;
node* next = head->next;
while(next->next != NULL){
free(head);
next = next->next;
head = next;
}
pthread_mutex_unlock(list->lock);
free(list);
}
void insert_value(list* list, int value)
{
// add code here
// if the list is empty
pthread_mutex_lock(list->lock);
if(list->head == NULL){
list->head->value = value;
pthread_mutex_unlock(list->lock);
}
else{
// init newnode
node* newNode = malloc(sizeof(node));
if(!newNode){
printf("malloc failed\n");
}
newNode->value = value;
newNode->next = NULL;
if(pthread_mutex_init(newNode->lock, NULL) != 0){
printf("mutex init failed\n");
}
node* curr = list->head;
// lock the list and the first node
pthread_mutex_lock(curr->lock);
if(curr->next == NULL){ // first and only node at the start of a list
if(curr->value > value){ // insert the newnode at the beggining
list->head = newNode;
newNode->next = curr;
}else{
curr->next = newNode;
}
pthread_mutex_unlock(list->lock);
pthread_mutex_unlock(curr->lock);
// finished the insert
}
else{
node* prev = curr;
curr = curr->next;
pthread_mutex_unlock(list->lock);
pthread_mutex_lock(curr->lock);
while(curr->value < value && curr->next != NULL){
pthread_mutex_unlock(prev->lock);
prev = curr;
curr = curr->next;
pthread_mutex_lock(curr->lock);
}
if(curr->next == NULL){
curr->next = newNode;
}else{
prev->next = newNode;
newNode->next = curr;
}
pthread_mutex_unlock(prev->lock);
pthread_mutex_unlock(curr->lock);
}
}
}
void remove_value(list* list, int value)
{
// add code here
}
void print_list(list* list)
{
// add code here
node* curr = list->head;
pthread_mutex_lock(list->lock);
if(curr != NULL){
pthread_mutex_unlock(list->lock);
while(curr != NULL){
pthread_mutex_lock(curr->lock);
print_node(curr);
curr = curr->next;
pthread_mutex_unlock(curr->lock);
}
}
printf("\n"); // DO NOT DELETE
}
void count_list(list* list, int (*predicate)(int))
{
int count = 0; // DO NOT DELETE
// add code here
printf("%d items were counted\n", count); // DO NOT DELETE
}
int main(){
list* l = create_list();
printf("1\n");
insert_value(l,6);
printf("2\n");
insert_value(l,12);
insert_value(l,3);
insert_value(l,19);
insert_value(l,8);
printf("3\n");
print_list(l);
printf("4\n");
delete_list(l);
}
concurrent_list.h:
typedef struct node node;
typedef struct list list;
list* create_list();
void delete_list(list* list);
void print_list(list* list);
void insert_value(list* list, int value);
void remove_value(list* list, int value);
void count_list(list* list, int (*predicate)(int));
The thrown error when compiling is:
Segmentation fault
Am I accessing illegal memory, not compiling correctly or am I using mutex threads wrong?
Any help would be appriciated.
create_list():
l->head = NULL will segfault if malloc failed. You probably want to return NULL; in addition to the print.
l->head->next = NULL; will always segfault as you set l->head to NULL
pthread_mutex_init(l->head->lock, NULL) will always segfault as l->head is NULL.
insert_value():
list->head->value = value; will segfault if list->head is NULL. You even ensure that it is with the if statement.
As you lock the list on modification (and you need to do that at least for the head changes) I eliminated the node lock. Inlined print_node() in print_list() as the former required caller to take a lock which is risky. Fixed create_list() per above. Simplified delete_list(). Fixed (per above) and simplified insert_value(). Removed dead code remove_value():
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct node node;
struct node {
int value;
node *next;
};
typedef struct list list;
struct list {
node *head;
pthread_mutex_t *lock;
};
list* create_list();
void delete_list(list* list);
list *insert_value(list* list, int value);
void print_list(list* list);
list* create_list() {
list* l = malloc(sizeof(*l));
if(!l) {
printf("malloc error");
return NULL;
}
l->head = NULL;
if(pthread_mutex_init(l->lock, NULL) != 0) {
printf("mutex init failed\n");
}
return l;
}
void delete_list(list *l) {
pthread_mutex_lock(l->lock);
while(l->head) {
node *n = l->head;
l->head = l->head->next;
free(n);
}
free(l);
pthread_mutex_unlock(l->lock);
}
list *insert_value(list *l, int value) {
node* newNode = malloc(sizeof(node));
if(!newNode){
printf("malloc failed\n");
return NULL;
}
newNode->value = value;
// head
pthread_mutex_lock(l->lock);
if(!l->head || value < l->head->value){
newNode->next = l->head;
l->head = newNode;
pthread_mutex_unlock(l->lock);
return l;
}
// non-head
node *n = l->head;
for(; n->next && value >= n->next->value; n = n->next);
newNode->next = n->next;
n->next = newNode;
pthread_mutex_unlock(l->lock);
return l;
}
void print_list(list *l) {
pthread_mutex_lock(l->lock);
for(node *n = l->head; n; n = n->next) {
printf("%d ", n->value);
}
pthread_mutex_unlock(l->lock);
printf("\n");
}
int main(){
list* l = create_list();
printf("1\n");
insert_value(l, 6);
printf("2\n");
insert_value(l,12);
insert_value(l,3);
insert_value(l,19);
insert_value(l,8);
printf("3\n");
print_list(l);
printf("4\n");
delete_list(l);
}
and the output is:
1
2
3
3 6 8 12 19
4

Reverse Every K Nodes

Every k nodes form a segment. If the last few nodes are less than K, then you can ignore them.
Write a reverseKnodes() which reserves every segment in the linked list.
The function prototype is given as follow: void reversekNodes(ListNode** head, int k);
Input format:
The 1st line is the k
The 2nd line is the data to create the linked list and ends with a non-digit symbol Example:
Input: 3 1 2 3 4 5 6 7 8 9 10 a
Output: 3 2 1 6 5 4 9 8 7 10
#include <stdio.h>
#include <stdlib.h>
struct _listNode
{
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList (ListNode * head);
void deleteList (ListNode ** ptrHead);
void reverseKNodes (ListNode ** head, int K);
int
main ()
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
scanf ("%d", &K);
while (scanf ("%d", &i))
{
if (head == NULL)
{
head = (ListNode *) malloc (sizeof (ListNode));
temp = head;
}
else
{
temp->next = (ListNode *) malloc (sizeof (ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes (&head, K);
printList (head);
deleteList (&head);
return 0;
}
void
printList (ListNode * head)
{
while (head != NULL)
{
printf ("%d ", head->item);
head = head->next;
}
printf ("\n");
}
void
deleteList (ListNode ** ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL)
{
temp = cur->next;
free (cur);
cur = temp;
}
*ptrHead = NULL;
}
void
reverseKNodes (ListNode ** head, int K)
{
struct _listNode *reverse (struct _listNode *head, int k){
if (!head)
return NULL;
struct _listNode* cur = head;
struct _listNode* next = NULL;
struct _listNode* prev = NULL;
int count = 0;
while (cur != NULL && count < K)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
}
I am not allowed to change anything else other than the void function for reverseKNodes and I know I have done it wrong but I dont know where I went wrong. Could someone help me please?
A few issues with nested functions ...
Nested functions aren't standard C.
Are of limited benefit.
They add extra overhead to invoke.
So [please] don't use them.
And, your usage of one is incorrect. In reverseKNodes, you define reverse but never call it. So, reverseKNodes is a no-op.
So, to fix, move reverse out of the body of reverseKNodes and place it above. Then, actually invoke it in reverseKNodes:
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the full refactored code. For aid in debug (e.g. with gdb), I added code to allow the program to take an input file as an argument.
#include <stdio.h>
#include <stdlib.h>
struct _listNode {
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList(ListNode *head);
void deleteList(ListNode **ptrHead);
void reverseKNodes(ListNode **head, int K);
int
main(int argc,char **argv)
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
--argc;
++argv;
FILE *xf;
do {
if (argc != 1) {
xf = stdin;
break;
}
xf = fopen(*argv,"r");
if (xf != NULL)
break;
perror(*argv);
exit(1);
} while (0);
fscanf(xf,"%d", &K);
while (fscanf(xf,"%d", &i)) {
if (head == NULL) {
head = (ListNode *) malloc(sizeof(ListNode));
temp = head;
}
else {
temp->next = (ListNode *) malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes(&head, K);
printList(head);
deleteList(&head);
return 0;
}
void
printList(ListNode *head)
{
while (head != NULL) {
printf("%d ", head->item);
head = head->next;
}
printf("\n");
}
void
deleteList(ListNode **ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL) {
temp = cur->next;
free(cur);
cur = temp;
}
*ptrHead = NULL;
}
struct _listNode *
reverse(struct _listNode *head, int K)
{
if (! head)
return NULL;
struct _listNode *cur = head;
struct _listNode *next = NULL;
struct _listNode *prev = NULL;
int count = 0;
while (cur != NULL && count < K) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the program output:
3 2 1 6 5 4 9 8 7 10

Stuck debugging a problem that I think is related to malloc

i'm currently writing a program that makes a sorted linked list in C. I'm new to C and am running into errors that I think are based around malloc. I would love if someone can give me some insight on my problems and how to fix them.
These are some of the errors I'm encountering:
==50002==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x563f6970912c bp 0x7ffe5d2b03f0 sp 0x7ffe5d2b03d0 T0)
==50002==The signal is caused by a WRITE memory access.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
struct node* next;
};
void insert(struct node** n_ref, struct node* newNode) {
struct node* curr;
if (*n_ref == NULL || (*n_ref)->data >= newNode->data) {
newNode->next = *n_ref;
*n_ref = newNode;
} else {
curr = *n_ref;
while (curr->next != NULL && curr->next->data < newNode->data) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
}
}
struct node* makeNode(int num){
struct node* newNode = NULL;
newNode->data = num;
newNode->next = NULL;
return newNode;
}
void delete(struct node** n, int num) {
struct node* temp = *n, *prev;
if (temp != NULL && temp->data == num) {
*n = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != num) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
return;
}
prev->next = temp->next;
free(temp);
}
void print(struct node* n) {
while (n != NULL) {
printf("%d ", n->data);
n = n->next;
}
}
int duplicate(struct node* n, int num) {
while (n != NULL) {
if (n->data == num) {
return 1;
}
n = n->next;
}
return 0;
}
int main(int argc, char* argv[argc + 1]) {
if (argc < 2) {
return EXIT_SUCCESS;
}
struct node* head = NULL;
FILE *file = fopen(argv[1], "r");
int x;
char str[10];
while (fscanf(file, "%s %d\n", str, &x) != EOF) {
if (strcmp(str, "INSERT")) {
if (duplicate(head, x) == 0) {
struct node* newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode = makeNode(x);
insert(&head, newNode);
}
} else if (strcmp(str, "DELETE")) {
delete(&head, x);
}
print(head);
}
fclose(file);
return EXIT_SUCCESS;
}
Sorry for it being so messy. This is my first post here.
The original posted makeNode() assigns a local pointer to NULL and dereferences that 2x. Suggest allocating space and check the result, eg:
struct node* makeNode(int num) {
struct node* newNode;
newNode = malloc(sizeof(*newNode));
if (newNode) {
newNode->data = num;
newNode->next = NULL;
}
return newNode;
}

make a list of m nodes, where m is taken in input

Hi this is the code I wrote for create as many nodes as he needs (the m variable), but I noticed that using this method I'm creating one more node. What's the best way of fcreating as many nodes as the user say us?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int val;
int rip;
struct Node *next;
} node;
node *modify(node *head);
void print(node *head2);
int main(){
int m, i;
printf("How many nodes: \n");
scanf("%d", &m);
node *head = NULL;
head = (node *)malloc(sizeof(node));
node *temp = head;
node *head2 = NULL;
printf("Write the value in HEAD position : \n");
scanf("%d", &temp->val);
temp->rip=0;
temp->next = NULL;
for(i=0; i < m-1; i++)
{
temp->next = (node *)malloc(sizeof(node));
printf("Write the value in position %d: \n", i);
temp = temp->next;
scanf("%d", &temp->val);
temp->rip=0;
temp->next = NULL;
}
head2 = modify(head);
print(head2);
return 0;
}
node *modify(node *head){
int counter, pass, m;
node *curr = head;
node *track = head;
node *precNode;
while (track != NULL){
counter = 0;
pass = 0;
m = track->val;
while( curr != NULL){
if(m == (curr)->val){
pass++;
counter++;
if(pass > 1){
node *removed = curr;
precNode->next = (curr)->next;
curr = (curr)->next;
free(removed);
}
if(pass == 1)
{
precNode = curr;
curr = curr->next;
}
}
else{
precNode = curr;
curr = (curr)->next;
}
}
track->rip = counter;
track = track->next;
curr = track;
}
return head;
}
void print(node *head2){
while(head2 != NULL){
printf("[%d, %d] -> ", head2->val, head2->rip);
head2 = head2->next;
}
printf("\n");
}
```

Printing a doubly linked list in reverse only printing first element

Writing a function to print a doubly linked list in reverse. The function stops after only printing 7 and does not print the rest of the items in the list. My programs and functions are below.
Edited to include code that didn't paste. Having issues copying and pasting with Putty my apologies.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node node;
void printRev(node* head);
node* removeNode(node* head, int d);
node* insertFront(node* head, int d);
node* insertBack(node* head, int d);
void print(node* head);
int max(node* head);
int min(node* head);
int locInList(node* head, int x);
int main()
{
node* head = NULL;
head = insertFront(head, 5);
head = insertFront(head, 4);
head = insertBack(head, 6);
head = insertBack(head, 7);
print(head);
printRev(head);
printf("Max: %d\n", max(head));
printf("Min: %d\n", min(head));
printf("locInList 5: %d\n", locInList(head, 5));
printf("locInList 9: %d\n", locInList(head, 9));
head = removeNode(head, 6);
print(head);
head = removeNode(head, 4);
print(head);
head = removeNode(head, 7);
print(head);
return 0;
}
void printRev(node* head) {
node *cur = head;
node *tmp = NULL;
if (cur == NULL) {
return;
}
else {
while(cur->next != NULL) {
cur = cur->next;
}
while(cur != NULL) {
printf("%d ", cur->data);
cur = cur->prev;
}
}
printf("\n");
}
node* removeNode(node* head, int d)
{
node *tmp = head->next;
head->data = head->next->data;
head->next = tmp->next;
free(tmp);
return head;
}
node* insertFront(node* head, int d)
{
node *tmp = NULL;
tmp = malloc(sizeof(node));
tmp->data = d;
tmp->next = head;
head = tmp;
return head;
}
node* insertBack(node* head, int d)
{
node *tmp = malloc(sizeof(node));
tmp->data = d;
tmp->next = NULL;
if(head == NULL){
return head;
}
}
else{
node *end = head;
while(end->next != NULL){
end = end->next;
}
end->next = tmp;
}
return head;
}
void print(node* head)
{
node *tmp = head;
while(tmp != NULL){
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
int max (node* head)
{
int max = head->data;
node *tmp = NULL;
tmp = head;
while(tmp->next != NULL){
if(tmp->data >= max){
max = tmp->data;
}
tmp = tmp->next;
}
}
return min;
}
int locInList(node* head, int x)
{
int i = 0;
node *tmp = NULL;
tmp = head;
while(tmp != NULL){
if(tmp->data == x){
return i;
}else{
i++;
tmp = tmp->next;
} }
return -1;
}
Expected results are - 7 6 5 4
Received results are - 7
Neither insertFront nor insertBack set prev, which is the root cause of your problem. (Your reverse iteration loop critically depends on the prev pointers having been set correctly.)
As it's a doubly linked list, you should point your head's back pointer to temp(new inserted one) in the function insertFront. So it should be ;
node* insertFront(node* head, int
d)
{
node *tmp = NULL;
tmp = malloc(sizeof(node));
tmp->data = d;
tmp->prev=NULL:
if(head==NULL)
return tmp;
head->prev=tmp;
tmp->next = head;
return tmp;
}
Similarly in insertBack function take care of making prev pointer point to previous node in the list.

Resources