In: Computer Science
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion;
Case 1:insertion in the beginning;
Case2:insertion in the end.
Once the list is printed after insertion; count the total number of names(nodes) in the link list and then,remove the name from the list (entered by the user at run time) and then print the list and count of nodes.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct Node Node;
struct Node {
char* name;
struct Node* next;
};
Node* head = NULL;
Node* curr = NULL;
Node* prev = NULL;
int count = 0;
void listAdd(char* word, bool toEnd)
{
Node* temp = head;
curr = head;
// point temp to the end
while (temp != NULL && temp->next != NULL)
temp = temp->next;
//create new node
Node* ptr = (Node*)malloc(sizeof(Node));
ptr->name = (char*)malloc(strlen(word) + 1);
strcpy(ptr->name, word); //store the word
ptr->next = NULL;
if (head == NULL) //if list is empty
{
head = ptr;
count++;
return;
}
if (toEnd) //if adding to the end
{
//temp is pointing to the end node
temp->next = ptr; //attach new node to the end
count++;
}
else //if adding to the start of list
{
ptr->next = head; //attach new node at the start
head = ptr; //now new node becomes head
count++;
}
}
void readWord(bool flag)
{
char buffer[100]; //to hold the name
printf("\nType end to stop:");
//read till end is typed
while (true) {
printf("\nEnter the name: ");
scanf("%s", buffer);
if (strcmp(buffer, "end") == 0) //if user types end
break; // stop adding to the list
listAdd(buffer, flag); //add the name
}
}
void remove_name(char* data)
{
if (head == NULL) {
printf("List is empty");
return;
}
if (strcmp(head->name, data) == 0) {
//if name to be deleted is at the start
if (head->next != NULL) {
//and there are more than 1 names
head = head->next; //remove the first
count--;
return;
}
else {
//and there is only one name in list i.e only head
head = NULL;
count--;
printf("List is empty now");
return;
}
}
else if (strcmp(head->name, data) != 0 && head->next == NULL) {
//if there is only one node(head) and its name doesn't match
printf("\n***** %s not found in the list! \n", data);
return;
}
curr = head;
while (curr->next != NULL && strcmp(curr->name, data) != 0) {
//loop and find name in the list start to end
//stop if name is found by curr->name
prev = curr;
curr = curr->next;
}
if (strcmp(curr->name, data) == 0) {
//if name is found in list (eg. A B C)
//if B is to be removed then point A->next to the B-next i.e C
//thus removes B
prev->next = curr->next;
//prev->next = prev->next->next; //this also can be used
count--;
free(curr);
}
else
printf("\n***** %s not found in the list! \n", data);
}
void printList()
{
Node* ptr = head;
printf("\n--------Friend List--------\n");
while (ptr) {
printf("%s ", ptr->name);
ptr = ptr->next; //set ptr to next node
}
printf("\ncount = %d ", count);
}
void freeList()
{
Node* ptr = head;
Node* tmp = 0;
//loop through the list and free each node
while (ptr) {
tmp = ptr->next;
free(ptr->name);
free(ptr);
ptr = tmp;
}
}
int main(void)
{
int option = 0;
char buffer[100];
printf("\n1. Insertion in the beginning");
printf("\n2. Insertion at the end");
printf("\nSelect your option: ");
scanf("%d", &option);
if (option == 1) {
readWord(false);
}
else if (option == 2) {
readWord(true);
}
else {
printf("\n*** Invalid option!!!");
}
printList();
printf("\nEnter name to remove: ");
scanf("%s", buffer);
remove_name(buffer);
printList();
freeList();
return 0;
}
Output------------------