Question is: Using at least 3 different types/pieces of information, define an a
ID: 3706386 • Letter: Q
Question
Question is: Using at least 3 different types/pieces of information, define an appropriate self-referential structure and manage the selected data using a linked list. A data item should have a unique identifier. Use a (case selection structure) to control the user selection from the following menu:
1) Store / Insert a data item in order
2) Remove a specific data item from the list
3) View a specific data item
4) Print a list of all the data items
5) Exit Application
My topic is about Members of a local gym. Any help is appreciated, thanks.
In C Program.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node //A linked list node
{
int data;
struct Node *next;
};
void insert(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); //allocate node
struct Node *last = *head_ref; /* used in step 5*/
new_node->data = new_data; //put in the data
new_node->next = NULL; //This new node is going to be the last node, so make next of it as NULL
if (*head_ref == NULL) //If the Linked List is empty, then make the new node as head
{
*head_ref = new_node;
return;
}
while (last->next != NULL) //Else traverse till the last node
last = last->next;
last->next = new_node; //Change the next of last node
return;
}
void deleteNode(struct Node **head_ref, int key)
{
struct Node* temp = *head_ref, *prev; // Store head node
if (temp != NULL && temp->data == key) // If head node itself holds the key to be deleted
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (temp != NULL && temp->data != key) // Search for the key to be deleted, keep track of the previous node as we need to change 'prev->next'
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return; // If key was not present in linked list
prev->next = temp->next; // Unlink the node from linked list
free(temp); // Free memory
}
bool search(struct Node* head, int x)
{
struct Node* current = head; // Initialize current
while (current != NULL)
{
if (current->key == x)
return true;
current = current->next;
}
return false;
}
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL; //Start with the empty list
while(1)
{
printf("1.Insert 2.Delete 3.View 4.Print 5.Exit choose your option... ");
int n;
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter no.: ");
int x;
scanf("%d",&x);
insert(&head, x);
break;
case 2:
printf("Enter no.: ");
int x;
scanf("%d",&x);
deleteNode(&head, x);
break;
case 3:
printf("Enter no.: ");
int x;
scanf("%d",&x);
if(search(&head, x))
printf("Matched ");
else
printf("Not Matched ");
break;
case 4:
printf("Created Linked List: ");
printList(head);
break;
case 5:
goto EndWhile;
default:
printf("ERROR: %d: Incorrect menu option ", n);
}
}
EndWhile: ;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.