Write a set of C functions to manipulate a linked list of jo

Write a set of C functions to manipulate a linked list of journal entry structs.

1. Create an Empty List

2. Insert an Element to the start. The function should take a pointer to an existing linked list, and (ii) a pointer to an existing journal entry struct, to be inserted at the start of the list.

3. Remove an element from the start. This function should take a pointer to an existing linked list, remove the first element from the list and return a pointer to it.

4. Print out the contents of the list.

5. Free the list.

*** Struct ****

typedef struct LinkedListNode{
   int data;
   struct LinkedListNode* next;

}LinkedListNode;

typedef struct{
   LinkedListNode* head;
}LinkedList;

Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct LinkedListNode
{
int data;
struct LinkedListNode *next;
};
typedef struct{
LinkedListNode* head;
}LinkedList;

struct LinkedListNode *current = NULL;

// create list
bool createList()
{
head == NULL;
}

//insert link at the first location
void insertFirst(int key, int data)
{
//create a link
struct LinkedListNode *link = (struct LinkedListNode*) malloc(sizeof(struct LinkedListNode));
  

link->data = data;
  
//point it to old first LinkedListNode
link->next = head;
  
//point first to new first LinkedListNode
head = link;
}

//delete first item
struct LinkedListNode* deleteFirst()
{

//save reference to first link
struct LinkedListNode *tempLink = head;
  
//mark next to first link as first
head = head->next;
  
//return the deleted link
return tempLink;
}

//display the list
void printList()
{
struct LinkedListNode *ptr = head;
printf(\"\ [ \");
  
//start from the beginning
while(ptr != NULL)
   {
printf(\"(%d,%d) \",ptr->key,ptr->data);
ptr = ptr->next;
}
  
printf(\" ]\");
}

//delete list

void deletelist()
{
free(head);
}

  

Write a set of C functions to manipulate a linked list of journal entry structs. 1. Create an Empty List 2. Insert an Element to the start. The function should
Write a set of C functions to manipulate a linked list of journal entry structs. 1. Create an Empty List 2. Insert an Element to the start. The function should

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site