Funtcion Interface Definition
typedef int ElementType;
typedef struct _dnode {
ElementType value;
struct _dnode *previous;
struct _dnode *next;
} DNode;
typedef DNode* DList;
DList initializeList();
void appendNode(DList list, ElementType value);
bool isEmpty(DList list);
void forwardTraverse(DList list);
void backwardTraverse(DList list);
DNode* findNode(DList list, ElementType value);
DNode* removeSingleNode(DList list, DNode* target);
void removeAllNodes(DList list, ElementType value);
void clearList(DList list);
void deallocateList(DList list);
Sample Test Program
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElementType;
typedef struct _dnode {
ElementType value;
struct _dnode *previous;
struct _dnode *next;
} DNode;
typedef DNode* DList;
DList initializeList();
void appendNode(DList list, ElementType value);
bool isEmpty(DList list);
void forwardTraverse(DList list);
void backwardTraverse(DList list);
DNode* findNode(DList list, ElementType value);
DNode* removeSingleNode(DList list, DNode* target);
void removeAllNodes(DList list, ElementType value);
void clearList(DList list);
void deallocateList(DList list);
int main() {
int input;
DList myList = initializeList();
scanf("%d", &input);
while (input != 0) {
appendNode(myList, input);
scanf("%d", &input);
}
forwardTraverse(myList);
backwardTraverse(myList);
scanf("%d", &input);
DNode *found = findNode(myList, input);
if (found != NULL) {
removeSingleNode(myList, found);
}
forwardTraverse(myList);
backwardTraverse(myList);
scanf("%d", &input);
removeAllNodes(myList, input);
forwardTraverse(myList);
backwardTraverse(myList);
deallocateList(myList);
return 0;
}
/* Implementation goes here */
Implementation Code
DList initializeList() {
DList head = (DList)malloc(sizeof(DNode));
if (head == NULL) exit(EXIT_FAILURE);
head->next = head;
head->previous = head;
return head;
}
void appendNode(DList list, ElementType value) {
DNode* newNode = (DNode*)malloc(sizeof(DNode));
newNode->value = value;
DNode* tail = list->previous;
newNode->previous = tail;
tail->next = newNode;
newNode->next = list;
list->previous = newNode;
}
bool isEmpty(DList list) {
return (list->next == list && list->previous == list);
}
void forwardTraverse(DList list) {
if (isEmpty(list)) {
printf("NULL");
return;
}
DNode* current = list->next;
while (current != list) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}
void backwardTraverse(DList list) {
if (isEmpty(list)) {
printf("NULL");
return;
}
DNode* current = list->previous;
while (current != list) {
printf("%d ", current->value);
current = current->previous;
}
printf("\n");
}
DNode* findNode(DList list, ElementType value) {
if (isEmpty(list)) return NULL;
DNode* current = list->next;
while (current != list) {
if (current->value == value) return current;
current = current->next;
}
return NULL;
}
DNode* removeSingleNode(DList list, DNode* target) {
target->previous->next = target->next;
target->next->previous = target->previous;
DNode* nextNode = target->next;
free(target);
return nextNode;
}
void removeAllNodes(DList list, ElementType value) {
DNode* current = list->next;
DNode* temp;
while (current != list) {
temp = current->next;
if (current->value == value) {
current->previous->next = current->next;
current->next->previous = current->previous;
free(current);
}
current = temp;
}
}
void clearList(DList list) {
DNode* current = list->next;
DNode* temp;
while (current != list) {
temp = current->next;
free(current);
current = temp;
}
list->next = list;
list->previous = list;
}
void deallocateList(DList list) {
clearList(list);
free(list);
}