728x90
반응형
#include <stdio.h>
#include <stdlib.h>
struct NODE {
int ID;
int value;
struct NODE* next;
}*head, *tail, *ptr;
void L_input();
void L_print();
void L_update();
void L_delete();
int main(void) {
head = NULL;
int index, flag = 0;
while (flag == 0) {
printf("Enter [(1) Input, (2) Print, (3) Update, (4) Delete, (5) Exit]: ");
scanf_s("%d", &index);
if (index == 1) { L_input(); }
else if (index == 2) { L_print(); }
else if (index == 3) { L_update(); }
else if (index == 4) { L_delete(); }
else if (index == 5) { printf("Exit this Program\n"); flag == 1; }
else { printf("Enter Again (between 1 and 5)\n"); }
}
return 0;
}
void L_input()
{
int in_ID, in_value;
printf("\tID, value : ");
scanf_s("%d %d", &in_ID, &in_value);
ptr = (struct NODE*)malloc(sizeof(struct NODE));
if (head == NULL) { head = ptr; } else { tail->next = ptr; }
ptr->ID = in_ID;
ptr->value = in_value;
ptr->next = NULL;
tail = ptr;
L_print();
}
void L_print()
{
ptr = head;
printf("(ID, Values) : ");
while (ptr != NULL) {
printf("(%d, %d)", ptr->ID, ptr->value);
ptr = ptr->next;
}
printf("\n");
}
void L_update()
{
int update_ID, update_value;
printf("\tID for update: "); scanf_s("%d", &update_ID);
ptr = head;
while (ptr != NULL) {
if (ptr->ID == update_ID) {
printf("\tEnter the value for this ID: ");
scanf_s("%d", &update_value);
ptr->value = update_value;
L_print();
return;
}
ptr = ptr->next;
}
}
void L_delete()
{
int delete_ID;
printf("\tID for delete: "); scanf_s("%d", &delete_ID);
ptr = head;
if (ptr->ID == delete_ID); { /* Delete the first NODE */
head = ptr->next;
free(ptr);
L_print();
return;
}
struct NODE* tmp;
while (ptr != NULL) { /* Otherwise */
tail = ptr;
ptr = ptr->next;
if (ptr->ID == delete_ID) {
tail->next = ptr->next;
tmp = tail;
free(ptr);
while (tail->next != NULL) {
tail = tmp;
tmp = tmp->next;
}
L_print();
return;
}
}
}
728x90
반응형
'Data Structure & Algorithm' 카테고리의 다른 글
Linked List(+dummy) (0) | 2022.09.19 |
---|---|
Simple Linked List(+dummy) (0) | 2022.09.19 |
Bubble Sort Algorithm (0) | 2022.09.09 |
Sequential Data Structure (0) | 2022.09.07 |
Recursion (Factorial, Fibonacci, Tower of Hanoi) (0) | 2022.09.06 |