Question: Data Structs C Lang Trying to figure out this code as much as possible. Can someone post more information on each function and how they
Data Structs C Lang Trying to figure out this code as much as possible. Can someone post more information on each function and how they work in either comments explaining the process or just text! The more clearer the better, really trying to understand this topic.
This is using Doubly Linked Lists with a .h file I'll include for proper syntax
//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;
}
}
/************************************************************************ DoublyLinkedList.h
Purpose: Define constants used in the project Struct definitions for a general Doubly Linked List. Define function prototypes used by general Doubly Linked Lists. ************************************************************************/ #include
//#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();
//Free the DoublyLinkedList and any nodes that still exist in the list void freeDoublyLinkedList(DoublyLinkedList list);
//Allocate a new node and store "value" as the Element in the node. Return the address of the node. NodeDL *allocateNode(Element value);
//Create a node to store the given Element and add it to the end of the DoublyLinkedList. void append(DoublyLinkedList list, Element value);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
