Last active
January 10, 2021 20:47
-
-
Save arn-ob/e7b5965db5bc149291a4 to your computer and use it in GitHub Desktop.
Main drive program to process a linked list of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define NULL 0 | |
struct list_element | |
{ | |
char item[40]; | |
struct list_element *next; | |
}; | |
typedef struct list_element node; | |
int menu(void); | |
void create(node *pt); | |
node *insert(node *pt); | |
node *remove(node *pt); | |
void display(node *pt); | |
void main(){ | |
node *start; | |
int choice; | |
do{ | |
choice = menu(); | |
start = (node *)malloc(sizeof(node)); // alocate some memory for start :) | |
switch (choice) | |
{ | |
case 1: | |
create(start); | |
printf_s("\n"); | |
display(start); | |
continue; | |
case 2: | |
start = insert(start); | |
printf_s("\n"); | |
display(start); | |
continue; | |
case 3: | |
start = remove(start); | |
printf_s("\n"); | |
display(start); | |
continue; | |
default: | |
printf_s("End of computaion"); | |
break; | |
} | |
} while (choice != 4); | |
} | |
int menu(){ | |
int choice; | |
do{ | |
printf_s("\nMain menu : \n\n"); | |
printf_s(" 1 - CREATE the link list \n "); | |
printf_s(" 2 - ADD a component \n "); | |
printf_s(" 3 - DELETE a component \n "); | |
printf_s(" 4 - END \n "); | |
printf_s("\n\nPlease Enter your choice (1,2,3,4) -> "); | |
scanf_s("%d", &choice); | |
if (choice < 1 || choice>4){ | |
printf_s("\n Error - Please try again "); | |
} | |
} while (choice < 1 || choice>4); | |
printf_s("\n\n"); | |
return(choice); | |
} | |
// Create a link list | |
void create(node *record){ | |
//argument points to the current node | |
printf_s("Data items (type \' END \' when finish) : "); | |
scanf_s("%s", record->item, 40); | |
if (strcmp(record->item, "END") == 0 || strcmp(record->item, "end") == 0){ | |
record->next = NULL; | |
} | |
else{ | |
// alocate space from next node | |
record->next = (node *)malloc(sizeof(node)); | |
create(record->next); | |
} | |
return; | |
} | |
// display the link list | |
void display(node *record){ | |
// argument points to the current node | |
if ( record->next != 0 ){ | |
printf_s("%s\n", record->item, 40); | |
display(record->next); | |
} | |
return; | |
} | |
node *insert(node *frist){ | |
/*add one componet to the linked list return a pointer to beginning of the modified list */ | |
// argu points to the frist node | |
node *locate(node*, char[]); | |
node *newrecord; | |
node *tag; | |
char newitem[40]; | |
char target[40]; | |
printf_s("New Data item : "); | |
scanf_s("%s", newitem, 40); | |
printf_s("Place before (type \'END\' if last) : "); | |
scanf_s("%s", target, 40); | |
if (strcmp(frist->item, target) == 0){ | |
// new node list in list | |
newrecord = (node *)malloc(sizeof(node)); | |
strcpy_s(newrecord->item, newitem); | |
newrecord->next = frist; | |
frist = newrecord; | |
} | |
else | |
{ | |
// insert new node after an existing node | |
tag = locate(frist, target); | |
if (tag == 0){ | |
printf_s("\n\nMatch not found ---- try again later "); | |
} | |
else{ | |
newrecord = (node *)malloc(sizeof(node)); | |
strcpy_s(newrecord->item, newitem); | |
newrecord->next = tag->next; | |
tag->next = newrecord; | |
} | |
} | |
return(frist); | |
} | |
//locate node | |
node *locate(node *record, char target[]){ | |
/* return a pointer to the node BEFORE the target node the frist argu point to the current node the second argu is the target strg */ | |
if (strcmp(record->next->item, target) == 0){ | |
return(record); | |
} else if(record->next->next==0){ | |
return(NULL); | |
} | |
else | |
{ | |
locate(record->next, target); | |
} | |
} | |
node *remove(node *frist){ | |
/* remove (delete) one component from the linked list return a pointer to beginning of the mdified list */ | |
// node the frist argu | |
node *locate(node*, char[]); | |
node *tag; | |
node *temp; | |
char target[40]; | |
printf_s("Data item to be delete : "); | |
scanf_s("%s", target, 40); | |
if (strcmp(frist->item, target) == 0){ | |
// del the frist node | |
temp = frist->next; | |
free(frist); | |
frist = temp; | |
} | |
else{ | |
// del a date item other then the frist | |
tag = locate(frist, target); | |
if (tag == 0){ | |
printf_s("\nMatch not found ------- Please try again later \n"); | |
} | |
else{ | |
temp = tag->next->next; | |
free(tag->next); | |
tag->next = temp; | |
} | |
} | |
return(frist); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment