Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Fabiola C. Villanueva
- Project: Linked List
- Time: July 9, 2023 (5:00pm)
- End: July 9, 2023 (7:42pm)
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define MENU 5
- typedef char STRING[50];
- //structure definitions
- typedef struct node {
- int data;
- struct node *next;
- } NodeType, *NodePtr;
- //function prototypes
- void insertSorted (NodePtr *list);
- int searchData (NodePtr list);
- void deleteAllData (NodePtr *list);
- void display (NodePtr list);
- int main() {
- system("CLS");
- NodePtr head = NULL;
- int i, item, choice, pos;
- STRING menu[MENU] = { "Exit",
- "Insert Sorted",
- "Search Data",
- "Delete All",
- "Display List" };
- do {
- printf("MENU\nKindly select a number:\n\n");
- for (i = 0; i < MENU; ++i) {
- printf(" [%d] %s\n", i, menu[i]);
- }
- printf("\nChoice: ");
- scanf("%d", &choice);
- system("CLS");
- switch (choice) {
- case 0:
- printf("\n\"EXIT\"\n\n");
- display(head);
- printf("\n\nTerminating program...");
- break;
- case 1:
- printf("\n\"INSERT SORTED\"\n\n");
- display(head);
- insertSorted (&head);
- printf("\n");
- display(head);
- break;
- case 2:
- printf("\n\"SEARCH DATA\"\n\n");
- display(head);
- pos = searchData (head);
- printf("Data is at position = [%d]\n\n", pos);
- display(head);
- break;
- case 3:
- printf("\n\"DELETE ALL\"\n\n");
- display(head);
- deleteAllData (&head);
- display(head);
- break;
- case 4:
- printf("\n\"DISPLAY LIST\"\n\n");
- display(head);
- break;
- }
- printf("\n\n");
- system("PAUSE");
- system("CLS");
- } while (choice != 0);
- printf("\n");
- return 0;
- }
- //function defenitions
- void insertSorted (NodePtr *list) { //a list from main will never enter this function unsorted
- NodePtr temp = (NodePtr) malloc (sizeof(NodeType)); //make temp node
- NodePtr *trav;
- int item;
- printf("\nEnter item to insert: "); //user input for data to insert
- scanf("%d", &item);
- if (temp != NULL) { //if temp is allocated
- temp->data = item; //populate temp data
- temp->next = NULL; //pre assign to NULL, overwrite later
- } //traverse until in right position
- for (trav = list; (*trav) != NULL && (*trav)->data < temp->data; trav = &(*trav)->next) {}
- temp->next = (*trav); //exits loop once item is in right position
- (*trav) = temp; //insert the temp node in place
- }
- int searchData (NodePtr list) { //return pos, if pos not found return -1
- NodePtr trav;
- int i, item, ret = -1;
- printf("\nEnter data to search: ");
- scanf("%d", &item);
- for (trav = list, i = 0; trav != NULL && trav->data != item; trav = trav->next, ++i) {
- if (trav->data == item) { //traverse and checking if item is found,
- ret = i; //loop automatically exits then assigns i to ret
- }
- } //if not found, ret stays -1
- return ret;
- }
- void deleteAllData (NodePtr *list) { //delete all occurences of user input item
- NodePtr *trav, temp;
- int item;
- printf("\nEnter item to delete: ");
- scanf("%d", &item);
- for (trav = list; (*trav) != NULL;) {
- if ((*trav)->data == item) { //if data in a node is found,
- temp = (*trav); //temp will get whole node to delete
- (*trav) = temp->next; //update for conditon when item found
- free(temp); //deletion of temp
- } else {
- trav = &(*trav)->next; //update to continue if not item
- }
- }
- }
- void display (NodePtr list) { //"pass by copy of list"; accessing via single pointer direct to first node
- printf("List = { ");
- for (; list != NULL; list = list->next) {
- printf("[%d]", list->data);
- if (list->next != NULL) {
- printf(" -> ");
- }
- }
- printf(" }");
- }
Advertisement
Add Comment
Please, Sign In to add comment