Last active
March 1, 2023 05:52
-
-
Save eroltutumlu/5299a8d4afc3330722d2 to your computer and use it in GitHub Desktop.
Singly linked list // 2
This file contains hidden or 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
/* | |
*http://stackoverflow.com/questions/23317646/why-is-strcpy-unsafe-in-c | |
We can use strncpy instead of strcpy. It's safer. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX_SIZE 10 | |
typedef struct teacherData{ | |
int teacherId; | |
char teacherName[MAX_SIZE]; | |
char teacherDesign[MAX_SIZE]; // Refer to Teacher Designation | |
struct teacherData *next; | |
}teacher; | |
void searchNode(teacher *listNode, int value); | |
teacher *deleteNode(teacher *listNode, int value); | |
void printLinkedList(teacher *listNode); | |
teacher *insertNewNode(teacher *listNode, int id, char name[], char designation[]); | |
char getOption(); | |
void menu(void); | |
int main() | |
{ | |
teacher *linkList; | |
int id; | |
char name[MAX_SIZE]; | |
char designation[MAX_SIZE]; | |
linkList = NULL; | |
static const char extValue = '5'; | |
menu(); | |
char choice; | |
do{ | |
switch(choice = getOption()) | |
{ | |
case '1' : printf("\n\nTeacher id: "); | |
scanf("%d",&id); | |
printf("Teacher name: "); | |
fflush(stdin); | |
gets(name); | |
printf("Teacher Designation: "); | |
gets(designation); | |
fflush(stdin); | |
linkList = insertNewNode(linkList,id,name,designation); | |
break; | |
case '2' : printf("\nId number: "); | |
scanf("%d",&id); | |
linkList = deleteNode(linkList,id); | |
break; | |
case '3' : printLinkedList(linkList); | |
break; | |
case '4' : printf("\nID: "); | |
scanf("%d",&id); | |
searchNode(linkList,id); | |
break; | |
case '5': break; | |
} | |
} | |
while(choice != extValue); | |
return 0; | |
} | |
void searchNode(teacher *listNode, int value){ | |
teacher *node; | |
node = listNode; | |
while(node != NULL){ | |
if(node->teacherId == value){ | |
printf("\nGood news, %d. teacher found.",value); | |
printLinkedList(node); | |
return; | |
} | |
node = node->next; | |
} | |
printf("\nSorry, Not found %d.teacher.. \n",value); | |
} | |
teacher *deleteNode(teacher *listNode, int value){ | |
teacher *node, *previosNode; | |
/*If node is in 1 record.*/ | |
node = listNode; | |
if(listNode->teacherId == value){ | |
printf("DELETED"); | |
node = listNode; | |
free(node); | |
listNode = listNode->next; | |
return listNode; | |
} | |
else{ | |
while(node != NULL && node->teacherId != value){ | |
previosNode = node; | |
node = node->next; | |
} | |
if(node == NULL){ | |
printf("\nDeletion failed.\n"); | |
return NULL; | |
} | |
else if (node->teacherId == value){ | |
previosNode->next = node->next; | |
free(node); | |
return listNode; | |
} | |
} | |
} | |
void printLinkedList(teacher *listNode){ | |
teacher *list; | |
list = listNode; | |
printf("\n\n Details..\n\n"); | |
while(list != NULL){ | |
printf("\nTeacher id: %d\nTeacher name: %s\nTeacher Designation: %s\n",list->teacherId,list->teacherName,list->teacherDesign); | |
list = list->next; | |
} | |
} | |
teacher *insertNewNode(teacher *listNode, int id, char name[], char designation[]){ | |
teacher *newNode = (teacher*)malloc(sizeof(teacher)); | |
if(newNode == NULL){ | |
printf("Memory mistake, sorry.."); | |
exit(1); | |
} | |
newNode->teacherId = id; | |
strcpy(newNode->teacherName,name); | |
strcpy(newNode->teacherDesign,designation); | |
newNode->next = listNode; /*Necessary for function which returns NULL to traverse.*/ | |
listNode = newNode; | |
return listNode; /*This statement always returns NULL, we know listNode's parameter function's arguments.*/ | |
} | |
/*Our menu which is showed by the computer*/ | |
void menu(){ | |
printf("----------------------------------\n"); | |
printf("1 - Insert node into the list\n"); | |
printf("2 - Delete a node in the list\n"); | |
printf("3 - Display the list\n"); | |
printf("4 - Search the list\n"); | |
printf("5 - EXIT\n"); | |
printf("----------------------------------\n"); | |
} | |
/*Returning option.*/ | |
char getOption(){ | |
char opt; | |
printf("\nEnter your option: "); | |
switch(opt=getch()){ | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': return opt; | |
default : printf("\nInvalid Choice.\n"); | |
} | |
return opt; // For example, when user entered '1' | |
// function returns this area so returned 1. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment