I'm working with a doubly linked list, writing a bunch of functions to do basic alterations to the list, but my functions for adding a node after an existing node isn't working, and my function for deleting a node at the head of the list isn't working either. Here's my code for these functions:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int Info;
typedef struct {
Info info;
} Item;
typedef Item* ItemRef;
typedef struct NodeStruct* NodeRef;
typedef struct NodeStruct {
ItemRef item;
NodeRef next;
NodeRef prev;
} Node;
typedef struct {
NodeRef head;
NodeRef tail;
int size;
} Dlist;
typedef Dlist* DlistRef;
void insertNodeAt(DlistRef dl, Info oldval, Info newval){
/* insert a new node with value newval after the node with value oldval */
/* if oldval is not found in the list, insert newval at the end of the list */
/* use findNode() to implement this function */
NodeRef x = findNode(dl, oldval);
NodeRef y;
NodeRef z = initializeNode(newval);
if(x != NULL){
if(x->next != NULL){
x->next = y;
z->next = x->next;
z->prev = x;
y->prev = z;
x->next = z;
}
if(x->next == NULL){
z->next = NULL;
z->prev = dl->tail;
x->next = z;
dl->tail = z;
}
}
if(x == NULL){
insertNodeAtTail(dl,newval);
}
dl->size++;
}
bool deleteNodeAtHead(DlistRef dl, Info* g){
/* delete new node at the beginning of the list */
/* return false if list is empty and true otherwise */
/* return value of deleted item in g */
NodeRef y = dl->head;
NodeRef z = y->next;
if(y==NULL){
return false;
}
if(y!=NULL){
dl->head = z;
z->prev = NULL;
y->next = NULL;
*g = y->item->info;
deallocateNode(y);
dl->size -= 1;
return true;
}
}
Any ideas why these aren't working?