Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structs C Lang Trying to figure out this code as much as possible. Can some

ID: 3592218 • Letter: D

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 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 <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();


//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);

Explanation / Answer

//Malloc a new DoublyLinkedListImp and return it's address.
DoublyLinkedList newDoublyLinkedList()
{

//Malloc is used to allocate Memory from Heap memory
DoublyLinkedList temp = (DoublyLinkedList)malloc(sizeof(DoublyLinkedListImp));

//Make pHead as NULL
temp->pHead = NULL;
//Make pFoot as NULL
temp->pFoot = NULL;

//return temp
return temp;

}

//Free the DoublyLinkedList and any nodes that still exist in the list
void freeDoublyLinkedList(DoublyLinkedList list)
{

//Point your Node to head Node
NodeDL *head = list->pHead;
NodeDL *temp;

//Iterate till the Next Node is not equal to NULL

while(head)
{

//Assign head to temp
temp = head;
//Increment node to head Node
head = head->pNext;
//free temp
free(temp);

}

//Make pHead as NULL
list->pHead = NULL;
//Make pFoot as 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)
{

//Malloc is used to allocate Memory from Heap memory
NodeDL *temp = (NodeDL*)malloc(sizeof(NodeDL));

//assign the value
temp->element = value;

//Make pNext as NULL
temp->pNext = NULL;
//Make pPrev as NULL
temp->pPrev = NULL;


//return temp

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)
{

//Allocate memory from HEAP using Malloc
NodeDL *temp = allocateNode(value);

//If it is the first NODE

if(list->pHead==NULL)
{

//The list is empty.
//Assigns HEAD to the first Node

list->pHead = temp;
//Assigns pFoot to the first Node
list->pFoot = list->pHead;

}
else
{

//The list is not empty.
//The list is not empty.

temp->pPrev = list->pFoot;

//Append temp to the last Node
list->pFoot->pNext = temp;
//Assigns Last Node to the foot
list->pFoot = temp;

}

}

//To display the list in forward direction

void display(DoublyLinkedList list)
{

//Assigns head to temp
NodeDL *temp = list->pHead;

//Run the loop till temp is not Equal to NULL
while(temp)
{

//Print the data value of NODE
printf("%s ",temp->element.szURL);
//Increment Temp to the next Node
temp = temp->pNext;

}

}

//To display the list in reverse direction using pPrev pointer

void display_reverse(DoublyLinkedList list)
{

//Assign temp to the Last Node of the Linked List
NodeDL *temp = list->pFoot;

//Run the loop till temp is not Equal to NULL
while(temp)
{

//Print the data value of NODE
printf("%s ",temp->element.szURL);
//Increment Temp to the previous Node

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 <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
{
//Store szURL
    char szURL[MAX_URL_LENGTH];
} Element;


//Typedef for a node in the doubly linked list (has next and previous pointers).
typedef struct NodeDL
{
//Stores the element
    Element element;
//Stores the next pointer
    struct NodeDL *pNext;
//Stores the previous pointer
    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
{
//pHead points the first Node of the List
    NodeDL *pHead;
//pFoot points the last Node of the List
    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);



I have added comments at each line of code , Let me know if you need more information. Comment and I will surely respond. Thanks