I need help writing linkedListWord.c which is a linked list (it is BOLD below).
ID: 3726476 • Letter: I
Question
I need help writing linkedListWord.c which is a linked list (it is BOLD below). linkedListWord.h is below and not bold. I can t use any helper either. Thank you.
linkedListWord.c
/* Include any required libraries. */
#include "linkedListWord.h"
/**
* Adds a new occurrence to the end of a list.
* tail - A pointer to the current tail of a list, NULL if it's empty.
* line - The line in which the word occurs
* lineNum - The number of that line within the file
* wordNum - The position of that word within that line
*
* Returns a pointer to the new tail of the list.
* (Include any additional required libraries at the top of linkedListWord.c. · Do not use any helper functions. · Your function must dynamically allocate memory for a new node. · Your function must initialize the new node’s member variables using the values passed as arguments)
*/
Node *addToTail(Node *tail, char *line, int lineNum, int wordNum) {
/* HELP HERE PLEASE*/
}
/**
* Removes an occurrence from the beginning of a list.
* head - A pointer to the current head of a list, NULL if it's empty
* line - A pointer at which to store the removed line
* lineNum - A pointer at which to store the removed line number
* wordNum - A pointer at which to store the removed word number
*
* Returns a pointer to the new head of the list, NULL if it's (now) empty.
* (Do not use any helper functions. · Your function must free the memory that was allocated (by addToTail) for the node being removed. · Your function must store the removed node’s member variables using the pointers passed as arguments. · You must use the given Node definition in linkedListWord.h)
*/
Node *rmFromHead(Node *head, char *line, int *lineNum, int *wordNum) {
/* HELP HERE PLEASE */
}
/**
* Prints out every node in a list.
* head - A pointer to the head of a list, NULL if it's empty
* (Do not use any helper functions. · Your function should not alter the list, only traverse the list and print out each of its nodes’ contents)
*/
void printList(Node *head) {
/* HELP HERE PLEASE */
}
_________________________________________________________________________________________________________________________________
linkedListWord.h
#define MAXLINE 101 /* The maximum length of a line, plus one. */
/**
* Represents a node in a linked list of word occurrences.
*/
typedef struct Node {
char line[MAXLINE]; /* The line in which the word occurs */
int lineNum; /* The number of that line within the file */
int wordNum; /* The position of the word within that line */
struct Node *next; /* The next node in the list */
} Node;
Node *addToTail(Node *, char *, int, int);
Node *rmFromHead(Node *, char *, int *, int *);
void printList(Node *);
Explanation / Answer
#define MAXLINE 101 /* The maximum length of a line, plus one. */
/**
* Represents a node in a linked list of word occurrences.
*/
typedef struct Node
{
char line[MAXLINE]; /* The line in which the word occurs */
int lineNum; /* The number of that line within the file */
int wordNum; /* The position of the word within that line */
struct Node *next; /* The next node in the list */
} Node;
Node *addToTail(Node *, char *, int, int);
Node *rmFromHead(Node *, char *, int *, int *);
void printList(Node *);
/* Include any required libraries. */
// #include "linkedListWord.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/**
* Adds a new occurrence to the end of a list.
* tail - A pointer to the current tail of a list, NULL if it's empty.
* line - The line in which the word occurs
* lineNum - The number of that line within the file
* wordNum - The position of that word within that line
*
* Returns a pointer to the new tail of the list.
* (Include any additional required libraries at the top of linkedListWord.c. · Do not use any helper functions. · Your function must dynamically allocate memory for a new node. · Your function must initialize the new node’s member variables using the values passed as arguments)
*/
Node *addToTail(Node *tail, char *line, int lineNum, int wordNum)
{
Node *temp = (Node *)malloc(sizeof(Node));
strcpy(temp->line, line);
temp->lineNum = lineNum;
temp->wordNum = wordNum;
temp->next = NULL;
if(tail==NULL){
tail = temp;
}
else{
tail->next = temp;
}
return temp;
/* HELP HERE PLEASE*/
}
/**
* Removes an occurrence from the beginning of a list.
* head - A pointer to the current head of a list, NULL if it's empty
* line - A pointer at which to store the removed line
* lineNum - A pointer at which to store the removed line number
* wordNum - A pointer at which to store the removed word number
*
* Returns a pointer to the new head of the list, NULL if it's (now) empty.
* (Do not use any helper functions. · Your function must free the memory that was allocated (by addToTail) for the node being removed. · Your function must store the removed node’s member variables using the pointers passed as arguments. · You must use the given Node definition in linkedListWord.h)
*/
Node *rmFromHead(Node *head, char *line, int *lineNum, int *wordNum)
{
if(head==NULL){
return NULL;
}
else{
Node *temp = head;
strcpy(line, temp->line);
*lineNum = temp->lineNum;
*wordNum = temp->wordNum;
head = head->next;
free(temp);
return head;
}
/* HELP HERE PLEASE */
}
/**
* Prints out every node in a list.
* head - A pointer to the head of a list, NULL if it's empty
* (Do not use any helper functions. · Your function should not alter the list, only traverse the list and print out each of its nodes’ contents)
*/
void printList(Node *head)
{
Node *temp = head;
while(temp!=NULL){
printf("%s Line Num: %d, word Num%d ", temp->line, temp->lineNum, temp->wordNum);
temp = temp->next;
}
/* HELP HERE PLEASE */
}
int main(){
Node *head = NULL, *tail=NULL;
head = addToTail(tail, "This is testing application", 1, 2);
tail = head;
tail = addToTail(tail, "This is testing application 2", 2, 4);
int lineNum, wordNum;
char line[1000];
head = rmFromHead(head, line, &lineNum, &wordNum);
printf(" Removed line: %s Line Num: %d, Word Num: %d ", line, lineNum, wordNum);
tail = addToTail(tail, "This is testing application 3", 3, 3);
printf("Display: ");
printList(head);
}