Data Structs C Using the following header file containing structs - make the fol
ID: 3593532 • Letter: D
Question
Data Structs C
Using the following header file containing structs - make the following functions. (I'd like to verify what I've done is relatable to someones code please, confused on the print function as well). I've added below the header file another .c file containing doubly linked lists which may be used here.
Question
a. BrowserList newBrowserList() which allocates the memory for a new BrowserList, initializes its variables, and returns its address.
b. void freeBrowserList(BrowserList browserList) which frees the memory used for the BrowserList (including its DoublyLinkedList).
c. void goToURL(BrowserList browserList, Element element) which will add a node in the DoublyLinkedList that stores element after pCurrentURL (any nodes that were in the list after pCurrentURL should be removed from the list) and updates pCurrentURL to point to the new node.
d. int back(BrowserList browserList) which moves pCurrentURL “back” one page. It returns TRUE if this completed successfully and returns false otherwise (e.g. the node doesn’t exist).
e. int forward(BrowserList browserList) which moves pCurrentURL “forward” one page. It returns TRUE if this completed successfully and returns false otherwise (e.g. the node doesn’t exist).
f. void printBrowserList(BrowserList browserList) which prints the contents of the BrowserList with one URL per line, placing *’s around the current URL. If we printed the final list in the example, we would have: www.google.com www.yahoo.com *www.utsa.edu*
Header:
Explanation / Answer
main.c
#include "BrowserList.h"
void readInFile(BrowserList list);
void removeNewline(char * pszInfix);
int main() {
BrowserList list = newBrowserList();
readInFile(list);
freeBrowserList(list);
}
/******************** readInFile **************************************
readInFile(BrowserList list)
Purpose:
Reads in the command list and processes the 4 following cases
PRINT Print the BrowserList
BACK Move pCurrentURL up the BrowserList
FORWARD Move pCurrentURL down the BrowserList
URL Insert the URL after pCurrentURL and then move
pCurrentURL forward
Parameters:
I BrowserList browserList The BrowserList struct
Return value:
void
Notes:
None
**************************************************************************/
void readInFile(BrowserList list) {
FILE *pfileCourse; // input stream file
char szInputBuffer[MAX_LINE_SIZE + 1]; // input buffer for fgets
pfileCourse = stdin;
while(fgets(szInputBuffer, MAX_LINE_SIZE, pfileCourse) != NULL) {
removeNewline(szInputBuffer);
if (strncmp("PRINT", szInputBuffer, 5) == 0) {
printBrowserList(list);
} else if (strncmp("BACK", szInputBuffer, 4) == 0) {
back(list);
} else if (strncmp("FORWARD", szInputBuffer, 7) == 0) {
forward(list);
} else {
add(list, szInputBuffer);
}
}
}
/******************** removeNewline **********************************
void removeNewline(char *)
Purpose:
remove at the end of cPassed token.
Parameters:
I char *pszInfix string that you want to remove the at the end of
Return value:
void
**************************************************************************/
void removeNewline(char * pszInfix) {
char *pos;
if ((pos=strchr(pszInfix, ' ')) != NULL)
*pos = '';
}
BrowserList.h
/***************************************************************
BrowserList.h
Purpose:
Defines structs for the BrowserList.
Defines prototypes for BrowserList functions.
Defines typedef for
BrowserListImp (Non-Pointer DLL Implimentation with Browser Wrapper)
BrowserList (Pointer of BrowserListImp)
Defines functions
BrowserList newBrowserList()
void freeBrowserList(BrowserList)
void goToURL(BrowserList, Element)
int back(BrowserList)
int forward(BrowserList)
void printBrowserList(BrowserList)
void add(BrowserList, char *)
Defines macros
MAX_LINE_SIZE
Notes:
This file's core philosophy is defining and programming around
BrowserListImp of a DoublyLinkedList
***************************************************************/
#include "DoublyLinkedList.h"
// #define constant values
#define MAX_LINE_SIZE 100 // Maximum number of character per read-in line
// typedef for a BrowserListImp which stores a DoublyLinkedList of all of the
// URLs visited plus a pointer to the node containing the current webpage.
typedef struct
{
DoublyLinkedList list;
NodeDL *pCurrentURL;
} BrowserListImp;
typedef BrowserListImp *BrowserList;
/*****Prototypes*******/
/******************** newBrowserList **************************************
BrowserList newBrowserList()
Purpose:
Allocates and builds an empty BrowserList.
Parameters:
None
Return value:
BrowserList
Notes:
Example Usage
BrowserList list = newBrowserList();
**************************************************************************/
BrowserList newBrowserList(){
BrowserList pBrowser = (BrowserList)(malloc(sizeof(pBrowser)));
pBrowser->list = newDoublyLinkedList();
pBrowser->pCurrentURL = NULL;
return pBrowser;
}
/******************** freeBrowserList **************************************
void freeBrowserList(BrowserList)
Purpose:
Frees the malloc'd space for a BrowserList
Parameters:
I BrowserList browserList The BrowserList struct
Return value:
void
Notes:
N/A
**************************************************************************/
void freeBrowserList(BrowserList browserList){
freeDoublyLinkedList(browserList->list);
free(browserList);
}
/******************** back **************************************
int back(BrowserList)
Purpose:
Move pCurrentURL back one NodeDL if pPrev exists.
Parameters:
I BrowserList browserList The BrowserList struct
Return value:
int
Notes:
None
**************************************************************************/
int back(BrowserList browserList) {
if (browserList->pCurrentURL->pPrev != NULL) {
browserList->pCurrentURL = browserList->pCurrentURL->pPrev;
return TRUE;
}
return FALSE;
}
/******************** forward **************************************
int forward(BrowserList)
Purpose:
Move pCurrentURL back one NodeDL if pPrev exists.
Parameters:
I BrowserList browserList The BrowserList struct
Return value:
int
Notes:
None
**************************************************************************/
int forward(BrowserList browserList) {
if (browserList->pCurrentURL->pNext != NULL) {
browserList->pCurrentURL = browserList->pCurrentURL->pNext;
return TRUE;
}
return FALSE;
}
/******************** goToURL **************************************
void goToURL(BrowserList, Element)
Purpose:
Given Element, goToURL finds the node with Element and sets pCurrentURL
to it, and if Element is not part of the BrowserList, then it does nothing.
Parameters:
I BrowserList browserList The BrowserList struct
I Element element Element containing the URL of a site
Return value:
void
Notes:
None
**************************************************************************/
void goToURL(BrowserList browserList, Element element){
insert(browserList->pCurrentURL, element);
forward(browserList);
}
/******************** printBrowserList **************************************
void printBrowserList(BrowserList)
Purpose:
Prints a BrowserList forwards with a * * around the pCurrentURL.
Parameters:
I BrowserList browserList The BrowserList struct
Return value:
void
Notes:
None
**************************************************************************/
void printBrowserList(BrowserList browserList) {
printLL(browserList->list->pHead, browserList->pCurrentURL);
}
/******************** add **************************************
void add(BrowserList, char *)
Purpose:
Use this instead of goToURL but performs the same function, except will
create the new BrowserList if there are no sites in it.
Parameters:
I BrowserList browserList The BrowserList struct
I char * szURL URL of a site
Return value:
void
Notes:
None
**************************************************************************/
void add(BrowserList browserList, char * site) {
Element * element = allocateElement(site);
if (browserList->list->pHead == NULL) {
append(browserList->list, * element);
browserList->pCurrentURL = browserList->list->pHead;
} else {
goToURL(browserList, * element);
}
free(element);
}
DoublyLinkedList.h
/************************************************************************
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.
Defines typedef for
Element
NodeDL (Structs Used as DoublyLinkedList Links)
DoublyLinkedListImp (Non-Pointer DLL Implimentation)
DoublyLinkedList (Pointer of DoublyLinkedListImp)
Defines functions
DoublyLinkedList newDoublyLinkedList()
void freeDoublyLinkedList(DoublyLinkedList)
NodeDL *allocateNode(Element)
void append(DoublyLinkedList, Element)
NodeDL * searchDL(NodeDL *, char *)
void printLL(NodeDL *, NodeDL *)
Element * allocateElement(char *)
void insert(NodeDL *, Element)
Defines macros
MAX_URL_LENGTH
TRUE
FALSE
Notes:
This file's core philosophy is defining and programming around
DoublyLinkedList operations and mindset.
************************************************************************/
#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*******/
/******************** newDoublyLinkedList **************************************
DoublyLinkedList newDoublyLinkedList()
Purpose:
Allocates and builds an empty DoublyLinkedList.
Parameters:
None
Return value:
DoublyLinkedList
Notes:
Example Usage
DoublyLinkedList list = newDoublyLinkedList();
**************************************************************************/
DoublyLinkedList newDoublyLinkedList() {
DoublyLinkedList DL = (DoublyLinkedList) (malloc(sizeof(DoublyLinkedListImp)));
DL->pHead = DL->pFoot = NULL;
return DL;
}
/******************** freeDoublyLinkedList **************************************
void freeDoublyLinkedList(DoublyLinkedList)
Purpose:
Frees the malloc'd space for a DoublyLinkedList
Parameters:
I DoublyLinkedList list The DoublyLinkedList struct
Return value:
void
Notes:
N/A
**************************************************************************/
void freeDoublyLinkedList(DoublyLinkedList list) {
NodeDL * pCurrent = list->pHead;
while (pCurrent != NULL) {
NodeDL * pNext = pCurrent->pNext;
free(pCurrent);
pCurrent = pNext;
}
free(list->pFoot);
free(pCurrent);
free(list);
}
/******************** allocateNode **************************************
NodeDL *allocateNode(Element)
Purpose:
Allocates and builds NodeDL for a DoublyLinkedList with passed element
Parameters:
I Element value Element containing a URL
Return value:
NodeDL
Notes:
N/A
**************************************************************************/
NodeDL *allocateNode(Element value) {
NodeDL * pNew = (NodeDL * )(malloc(sizeof(NodeDL)));
pNew->element = value;
pNew->pNext = NULL;
pNew->pPrev = NULL;
return pNew;
}
/******************** append **************************************
void append(DoublyLinkedList, Element)
Purpose:
Inserts a NodeDL, malloc'd from passed Element, at the end
of a DoublyLinkedList, pFoot.
Parameters:
I DoublyLinkedList list The DoublyLinkedList struct
I Element value Element containing a URL
Return value:
void
Notes:
This differs from insert, where insert can be anywhere, this is
specifically at the end of the list.
**************************************************************************/
void append(DoublyLinkedList list, Element value) {
NodeDL * pNew = allocateNode(value);
pNew->pNext = list->pHead;
if (list->pHead != NULL)
list->pHead->pPrev = pNew;
list->pHead = pNew;
}
/******************** searchDL **************************************
NodeDL * searchDL(NodeDL *, char *)
Purpose:
Given szURL, searchDL finds the node with szURL and returns it,
and if szURL is not part of the DoublyLinkedList, then it returns NULL
Parameters:
I NodeDL * pNode Container for the Element
I char * szURL URL of a site
Return value:
NodeDL *
Notes:
None
**************************************************************************/
NodeDL * searchDL(NodeDL * pNode, char * szURL) {
if (pNode == NULL) {
return NULL;
} else if (strcmp(pNode->element.szURL, szURL) == 0) {
return pNode;
} else {
return searchDL(pNode->pNext, szURL);
}
}
/******************** printLL **************************************
void printLL(NodeDL *, NodeDL *)
Purpose:
Prints a DoublyLinkedList forwards with a * * around the NodeDL matching
the second NodeDL, pCurrent, passed.
Parameters:
I NodeDL * pNode Container for the Element
I NodeDL * pCurrent Container for the Element
Return value:
void
Notes:
None
**************************************************************************/
void printLL(NodeDL * pNode, NodeDL * pCurrent) {
if (pNode != NULL) {
if (pNode == pCurrent) {
printf("*%s", pNode->element.szURL);
printf("* ");
} else {
printf("%s ", pNode->element.szURL);
}
printLL(pNode->pNext, pCurrent);
} else {
printf(" ");
}
}
/******************** allocateElement **************************************
Element * allocateElement(char *)
Purpose:
Allocates and builds an Element.
Parameters:
I char * szURL URL of a site
Return value:
Element
Notes:
None
**************************************************************************/
Element * allocateElement(char * szURL) {
Element * element = (Element *) malloc(sizeof(Element));
strcpy(element->szURL, szURL);
return element;
}
/******************** allocateElement **************************************
Element * allocateElement(char *)
Purpose:
Inserts a NodeDL after the passed in NodeDL, pNode, in a DoublyLinkedList.
Parameters:
I NodeDL * pNode Node to make insertion after
I Element value Element to insert after pNode
Return value:
void
Notes:
None
**************************************************************************/
void insert(NodeDL * pNode, Element value) {
NodeDL * pNew = allocateNode(value);
if (pNode != NULL) {
pNew->pNext = pNode->pNext;
pNode->pNext = pNew;
pNew->pPrev = pNode;
if (pNew->pNext != NULL)
pNew->pNext->pPrev = pNew;
}
}
input.txt
www.google.com
www.yahoo.com
www.utsa.edu
PRINT
BACK
PRINT
FORWARD
www.cs.utsa.edu
BACK
www.msn.com
PRINT
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.