I need help writing linkedListWord.c which is a linked list (it is BOLD below).
ID: 3731136 • 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 cant use any helper functions and it needs to compile with c. Thank you.
linkedListWord.c.
/* Include any required libraries. */
#include "linkedListWord.h"
Node *addToTail(Node *tail, char *line, int lineNum, int wordNum)
{
/* HELP HERE PLEASE*/
* 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.
}
Node *rmFromHead(Node *head, 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.
}
void printList(Node *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*/
}
_________________________________________________________________________________________________________________________________
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
//linkedListWord.c is given below. If you found this helpful, please leave like. Explanations to the code is given in the comments.
#include<stdio.h>
#include<stdlib.h>
#include "linkedListWord.h"
Node *addToTail(Node *tail, char *line, int lineNum, int wordNum)
{
Node *temp=(Node*)malloc(sizeof(Node)); //Dynamically creating a Node with given values.
strcpy(temp->line,line); //Initializing the created Node.
temp->lineNum=lineNum;
temp->wordNum=wordNum;
temp->next=NULL;
if(tail==NULL)
return temp;
tail->next=temp; //Linking previous tail to new tail
return temp; //Returning new tail
}
Node *rmFromHead(Node *head, char *line, int *lineNum, int *wordNum)
{
if(head==NULL)
return NULL;
strcpy(line,head->line); //Storing values of old head to given pointers
*lineNum=head->lineNum;
*wordNum=head->wordNum;
if(head->next==NULL)
return NULL;
head=head->next; //Removing the old head and updating
return head; //Returning new head
}
void printList(Node *head)
{
Node *temp=head;
while(temp!=NULL) //Traversing through linked list.
{
printf("line:%s lineNum:%d wordNum:%d ",temp->line,temp->lineNum,temp->wordNum); //Printing all the Nodes
temp=temp->next;
}
return;
}