상세 컨텐츠

본문 제목

Single LinkedList - 간단한 성적처리 C언어

자료구조

by ChrisMare 2018. 1. 26. 17:56

본문

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

const char scoName[5][10] = { "Kor","Eng","Mat","Total","Avg" };


typedef struct card {

char name[10];

int score[4];

float avg;

struct card *next; // 다음 노드를 가르키는 용도

}Card;


void newNode(Card **new1);

void InsertNode(Card **head, Card **new1, Card **cur);

void deleteNode(Card **head, Card **cur, Card **del);

void modifyNode(Card **head, Card **cur);

void searchNode(Card **head, Card **cur);

void all_Print(Card **head, Card **cur);


void main() {

Card *head; // 첫번째를 가르킬 때, 첫번째를 삭제할 때만 사용

Card *new1; // 새로운 노드를 가르킬 때

Card *del; // 삭제할 때 사용

Card *cur; // 위의 나머지 모든곳에 사용!!


head = cur = new1 = del = NULL;

int btn = 0;

// 새로운 노드잡음

while (1) {

printf(":::::::::::::::::::::::: M e n u :::::::::::::::::::::\n");

printf("::1.추가  2.삭제  3.수정  4.검색  5.전체출력  6.종료::\n");

printf("::::::::::::::::::::::::::::::::::::::::::::::::::::::\n");

scanf("%d", &btn);

switch (btn)

{

case 1:

// Insert part

InsertNode(&head, &new1, &cur);

break;

case 2:

// delete part

deleteNode(&head, &cur, &del);

break;

case 3:

//modify part

modifyNode(&head, &cur);

break;

case 4:

//select part

searchNode(&head, &cur);

break;

case 5:

//Print part

all_Print(&head, &cur);

break;

case 6:

printf("종료\n");

exit(-1);

default:

printf("잘못입력하셨네여~...^^;;");

printf("다시입력해주세요!!\n");

break;

}

}

}


void newNode(Card **new1) {

// 새로운 노드잡음

if ((*new1 = (Card*)calloc(1, sizeof(Card))) == NULL) {

puts("메모리 할당 실패");

exit(-1);

}

printf("Name Input : ");

scanf("%s", (*new1)->name);

for (int i = 0; i < 3; i++) {

printf("%s Input : ", scoName[i]);

scanf("%d", &(*new1)->score[i]);

(*new1)->score[3] += (*new1)->score[i];

}

(*new1)->avg = (*new1)->score[3] / 3.f;


(*new1)->next = NULL;

}


void InsertNode(Card **head, Card **new1, Card **cur) {

int pos = 1;

int cnt = 0;

if (*head == NULL) {

printf("데이터가 없네요? 처음에 삽입할께요!!\n");

newNode(new1);

*head = *new1;

cnt++;

}

else {

printf("몇 번째에 삽입할거야?\n==>");

scanf("%d", &pos);

*cur = *head;

newNode(new1);

cnt++;

if (pos == 1) {

(*new1)->next = (*cur);

(*head) = (*new1);

}

else if (pos > 1) {

for (int i = 1; i < pos - 1; i++) {

(*cur) = (*cur)->next;

}

(*new1)->next = (*cur)->next;

(*cur)->next = (*new1);

}

}

if (!cnt) {

printf("삽입 성공!!\n");

}

}


void deleteNode(Card **head, Card **cur, Card **del) {

char name[10] = "";

printf("어떤분을 삭제하실래여?\n==> ");

scanf("%s", name);

*cur = *head;

int cnt = 0;

int cnt2 = 0;


while ((*cur) != NULL)

{

cnt++;

if (!strcmp((*cur)->name, name)) {

if (cnt == 1) {

(*del) = (*head);

(*head) = (*head)->next;

(*cur) = (*head);

(*del)->next = NULL;

}

else {

(*cur) = (*head);

for (int i = 0; i < cnt - 2; i++) {

(*cur) = (*cur)->next;

}

(*del) = (*cur)->next;

(*cur)->next = (*del)->next;

(*del)->next = NULL;

}

cnt=0;

cnt2++;

free(*del);

continue;

}

*cur = (*cur)->next;

}

if (cnt2) {

printf("삭제 성공!!\n");

}

else {

printf("그런사람 없어요!!!\n");

}

}

void modifyNode(Card **head, Card **cur) {

int cnt = 0;

int id[10] = { 0 };

char name[10] = "";

int i = 0;

printf("수정하실 이름을 적어주세요!!\n==>");

scanf("%s", name);

(*cur) = (*head);

while ((*cur) != NULL) {

if (!(strcmp((*cur)->name, name))) {

if (i == 0)

printf("Name\tKor\tEng\tMat\tTotal\tAvg\n");

printf("%s\t", (*cur)->name);

for (int i = 0; i < 4; i++)

printf("%d\t", (*cur)->score[i]);

printf("%.2f\n", (*cur)->avg);

id[i++] = cnt;

}

cnt++;

(*cur) = (*cur)->next;

}

printf("수정하실 번호를 선택해주세요!!\n");

for (int a = 1; a <= i; a++) {

printf("%d번\t", a);

}

puts("");

int bt = 0;

printf("몇 번? ==> ");

scanf("%d", &bt);

(*cur) = (*head);

for (int a = 0; a < id[bt - 1]; a++) {

(*cur) = (*cur)->next;

}

printf("Modifing....\n");

printf("Name Input : ");

scanf("%s", (*cur)->name);

(*cur)->score[3] = 0;

for (int i = 0; i < 3; i++) {

printf("%s Input : ", scoName[i]);

scanf("%d", &(*cur)->score[i]);

(*cur)->score[3] += (*cur)->score[i];

}

(*cur)->avg = (*cur)->score[3] / 3.f;

}


void searchNode(Card **head, Card **cur) {

char name[10] = "";

int cf = 0;

printf("검색할 이름이 뭔가요?\n==>");

scanf("%s", name);

(*cur) = (*head);

while ((*cur) != NULL) {

if (!(strcmp((*cur)->name, name))) {

printf("Name\t\Kor\tEng\tMat\tTotal\tAvg\n");

printf("%s\t", (*cur)->name);

for (int i = 0; i < 4; i++) {

printf("%d\t", (*cur)->score[i]);

}

printf("%.2f\n", (*cur)->avg);

cf++;

}

(*cur) = (*cur)->next;

}

if (cf == 0)

printf("그런분 없네여....^^\n");

}


void all_Print(Card **head, Card **cur) {

printf("전체출력\n");

printf("Name\t\Kor\tEng\tMat\tTotal\tAvg\n");

(*cur) = (*head);

while ((*cur) != NULL) {

printf("%s\t%", (*cur)->name);

for (int i = 0; i < 4; i++) {

printf("%d\t", (*cur)->score[i]);

}

printf("%.2f\n", (*cur)->avg);

(*cur) = (*cur)->next;

}

}

'자료구조' 카테고리의 다른 글

희소행렬(SparseMatrix) 연산 - 덧셈  (0) 2017.10.01
SparseMatrix(희소행렬) - 정의  (0) 2017.09.30

관련글 더보기

댓글 영역