Data Structures C Language Question Using what is implemented in the \"DoublyLin
ID: 3591682 • Letter: D
Question
Data Structures C Language Question
Using what is implemented in the "DoublyLinkedList.h" file provided below implement the following functions
a. DoublyLinkedList newDoublyLinkedList() which should allocate the memory for a new doubly linked list, initialize the variables, and return the address.
b. void freeDoublyLinkedList(DoublyLinkedList list) which should free the linked list (including any nodes currently in the list).
c. NodeDL *allocateNode(Element value) which should allocate memory for a new node, store “value” inside this node, and return the address of the node.
d. void append(DoublyLinkedList list, Element value) which should add a new node (which stores value) to the end of the list. Notice that often one would want to insert into the middle of a linked list, but for this project it suffices to only insert at the end of a linked list.
/************************************************************************
DoublyLinkedList.h
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
//#define constant values
#define MAX_URL_LENGTH 50
#define TRUE 1
#define FALSE 0
//typedef for the Element struct which constains a c string to store a URL in the BrowserList
typedef struct
{
char szURL[MAX_URL_LENGTH];
} Element;
//Typedef for a node in the doubly linked list (has next and previous pointers).
typedef struct NodeDL
{
Element element;
struct NodeDL *pNext;
struct NodeDL *pPrev;
} NodeDL;
//Typedef for a doubly linked list implementation.
//Contains a pointer to the first node in the list and the last node in the list (pHead and pFoot respectively).
typedef struct
{
NodeDL *pHead;
NodeDL *pFoot;
} DoublyLinkedListImp;
typedef DoublyLinkedListImp *DoublyLinkedList;
/*****Prototypes*******/
//Malloc a new DoublyLinkedListImp and return it's address.
DoublyLinkedList newDoublyLinkedList(){
DoublyLinkedList temp = (DoublyLinkedList)malloc(sizeof(DoublyLinkedListImp));
temp->pHead = NULL;
temp->pFoot = NULL;
return temp;
}
//Free the DoublyLinkedList and any nodes that still exist in the list
void freeDoublyLinkedList(DoublyLinkedList list){
NodeDL * head = list->pHead;
NodeDL * temp;
while(head){
temp = head;
head = head->pNext;
free(temp);
}
list->pHead = NULL;
list->pFoot = NULL;
}
//Allocate a new node and store "value" as the Element in the node. Return the address of the node.
NodeDL *allocateNode(Element value){
NodeDL * temp = (NodeDL *)malloc(sizeof(NodeDL));
temp->element = value;
temp->pNext = NULL;
temp->pPrev = NULL;
return temp;
}
//Create a node to store the given Element and add it to the end of the DoublyLinkedList.
void append(DoublyLinkedList list, Element value){
NodeDL * temp = allocateNode(value);
if(list->pHead==NULL){
//The list is empty.
list->pHead = temp;
list->pFoot = list->pHead;
}
else{
//The list is not empty.
temp->pPrev = list->pFoot;
list->pFoot->pNext = temp;
list->pFoot = temp;
}
}
//To display the list in forward direction
void display(DoublyLinkedList list){
NodeDL * temp = list->pHead;
while(temp){
printf("%s ",temp->element.szURL);
temp = temp->pNext;
}
}
//To display the list in reverse direction using pPrev pointer
void display_reverse(DoublyLinkedList list){
NodeDL * temp = list->pFoot;
while(temp){
printf("%s ",temp->element.szURL);
temp = temp->pPrev;
}
}
int main(){
//
DoublyLinkedList list = newDoublyLinkedList();
Element ele;
strcpy(ele.szURL,"ABC");
append(list,ele);
strcpy(ele.szURL,"ABCD");
append(list,ele);
printf("Contents of the list in forward direction: ");
display(list);
printf("Contents of the list in reverse direction: ");
display_reverse(list);
}
Output:
Contents of the list in forward direction:
ABC
ABCD
Contents of the list in reverse direction:
ABCD
ABC
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.