Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include /*True or False declaration*/ #define FALSE 0 #define TRUE 1 /*Node declaration of a Linked List*/ struct Node { int val; struct Node

#include  #include  /*True or False declaration*/ #define FALSE 0 #define TRUE 1 /*Node declaration of a Linked List*/ struct Node { int val; struct Node *next; }; /*List declaration*/ struct ListRecord{ struct Node *head; struct Node *tail; int size; }; /*Function signatures*/ struct ListRecord* CreateList(void); void MakeEmptyList(struct ListRecord*); int ListSize(struct ListRecord*); int HeadOfList(struct ListRecord*); int TailOfList(struct ListRecord*); int IsEmptyList(struct ListRecord*); void DisplayList(struct ListRecord*); void InsertList(struct ListRecord*, int, int); int main() { char command; int exit = FALSE; int pos, val; struct ListRecord *myList; myList=CreateList(); while (!exit) { fflush(stdin); printf("i: Initialise "); printf("n: Insert new node "); printf("x: Exit "); printf("Enter command: "); scanf("%c", &command); fflush(stdin); switch (command) { case 'i': MakeEmptyList(myList); DisplayList(myList); break; case 'n': printf("enter value: "); scanf("%d", &val); printf("enter position: "); scanf("%d", &pos); InsertList(myList, pos, val); DisplayList(myList); break; case 'x': exit = TRUE; break; default: printf("Command is not recognized! "); break; } } printf(" "); system("PAUSE"); return 0; } //A function that creates a list struct ListRecord * CreateList() { struct ListRecord * l; l = (struct ListRecord *) malloc(sizeof(struct ListRecord)); if (l == NULL){ printf("Out of memory! "); return NULL; } else{ MakeEmptyList(l); return l; } } //A function that makes the head and tail NULL of a given list void MakeEmptyList(struct ListRecord * l) { l->head = NULL; l->tail = NULL; l->size = 0; } //A function that checks if a list is empty int IsEmptyList(struct ListRecord * l) { return (l->size == 0); } //A function that returns the list size int ListSize(struct ListRecord * l) { return (l->size); } //A function that returns the value stored in the list int HeadOfList(struct ListRecord * l) { if (!IsEmptyList(l)) return l->head->val; else { printf("The linked list is empty "); return -1; } } //A function that returns the value stored in the tail of the list int TailOfList(struct ListRecord * l) { if (!IsEmptyList(l)) return l->tail->val; else { printf("The linked list is empty "); return -1; } } /*A function that traverses a linked list and prints the values*/ void DisplayList(struct ListRecord *l) { struct Node *p; p = l->head; printf("List content: "); while (p != NULL) { printf("--> %d\t", p->val); p = p->next; } printf(" "); } void InsertList(struct ListRecord * l, int pos, int val) { //COMPLETE THIS } 

- Write a function called InsertList() which given a list, a position and a value creates a node to store the given value and inserts this node to the given position.

- Write a function called DeleteList() which given a value finds the node in the list with that value and deletes that node from the list.

- Write a function called GetElementAtPosition() which given a list and a position value, finds the node at that position and returns the value stored in the node in that position.

- Write a function called GetPositionOfElement() which given a list and a value, finds the position of the node that stores that given value.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions