- Write a function called InsertList() which given a list, a position and a valu
ID: 3874590 • Letter: #
Question
- 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.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
/*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);
void DeleteList(struct ListRecord*, int);
int GetElementAtPosition(struct ListRecord*, int);
int GetPositionOfElement(struct ListRecord*, 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("d: Delete new node ");
printf("e: Element at position ");
printf("p: Position of element ");
printf("x: Exit ");
printf("Enter command: ");
scanf("%c", &command);
fflush(stdin);
switch (command)
{
case 'i':
MakeEmptyList(myList);
break;
case 'n':
printf("enter value: ");
scanf("%d", &val);
printf("enter position: ");
scanf("%d", &pos);
InsertList(myList, pos, val);
DisplayList(myList);
break;
case 'd':
printf("Enter Value to delete: ");
scanf("%d", &val);
DeleteList(myList, val);
break;
case 'e':
printf("Enter position (starts at 0): ");
scanf("%d", &pos);
printf("Element at position %d is %d ", pos, GetElementAtPosition(myList, pos));
break;
case 'p':
printf("Enter Value to find : ");
scanf("%d", &val);
printf("Value %d is at position %d ", val, GetPositionOfElement(myList, val));
break;
case 'x':
exit = TRUE;
break;
default:
printf("Command is not recognized! ");
break;
}
DisplayList(myList);
}
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 ", p->val);
p = p->next;
}
printf(" ");
}
//position is the index where to insert starting from 0
void InsertList(struct ListRecord * l, int pos, int val)
{
struct Node *curr = l->head;
struct Node *prev = NULL;
int index = 0;
struct Node *n = malloc(sizeof(struct Node));
n->val = val;
n->next = NULL;
if(l->size == 0)
{
l->head = l->tail = n;
l->size = 1;
return;
}
//out of bounds?, set to proper values
if(pos < 0)
pos = 0;
else if (pos >= l->size)
pos = l->size;
for(index = 0; index < pos; index++)
{
prev = curr;
curr = curr->next;
}
n->next = curr;
if(prev == NULL) //insert as 1st node?
l->head = n;
else
prev->next = n;
if(n->next == NULL) //was inserted as last ?
l->tail = n;
l->size++;
}
void DeleteList(struct ListRecord* l, int val)
{
struct Node *curr = l->head;
struct Node *prev = NULL;
int found = 0;
while(curr != NULL)
{
if(curr->val == val) //found
{
found = 1;
break;
}
prev = curr;
curr = curr->next;
}
if(found)
{
if(prev == NULL) //delete 1st node?
{
l->head = curr->next;
if(l->head == NULL) //list is now empty?
l->tail = NULL;
}
else
{
prev->next = curr->next;
if(l->tail == curr) //delete tail node?
{
l->tail = prev;
}
}
free(curr);
l->size--;
}
}
//pos is the index starting from 0
int GetElementAtPosition(struct ListRecord* l, int pos)
{
struct Node *curr = l->head;
int index = 0;
for(index = 0; index < pos && curr != NULL; index++)
{
curr = curr->next;
}
if(curr == NULL) //not found position
return -1;
else
return curr->val;
}
int GetPositionOfElement(struct ListRecord* l , int val)
{
struct Node *curr = l->head;
int index = 0;
while(curr != NULL)
{
if(curr->val == val)
return index;
curr = curr->next;
index++;
}
//not found position
return -1;
}
output
======
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: i
List content:
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: n
enter value: 2
enter position: 0
List content:
--> 2
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: n
enter value: 4
enter position: 1
List content:
--> 2 --> 4
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: e
Enter position (starts at 0): 1
Element at position 1 is 4
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: p
Enter Value to find : 5
Value 5 is at position -1
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: p
Enter Value to find : 2
Value 2 is at position 0
List content:
--> 2 --> 4
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: d
Enter Value to delete: 4
i: Initialise
n: Insert new node
d: Delete new node
e: Element at position
p: Position of element
x: Exit
Enter command: x
List content:
--> 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.