Single LinkedList Exercise2
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
typedef struct Node
{
int data;
struct Node* next;
}Node;
void disp(struct Node *root);
int Insert(struct Node*root, int position); //추가 , Index [position]에 삽입한다.
int Delete(struct Node*root, int position); //삭제, Index [position]을 삭제한다.
int SetData(struct Node*root, int position, int data); // Index [position]에 data를 set
int GetData(struct Node*root, int position, int* data); // Index [position]에 data를 get
#define ASSERT(exp) if(!(exp)){puts("Err\n");}
void main()
{
Node* root = (Node*)malloc(sizeof(Node));
root->next = NULL;
int r, n;
// 0번위치에 10개 삽입
for (int i = 0 ; i < 10 ; i++)
{
r = Insert(root, 0); ASSERT(r); // 0번째위치에 노드만들기
r = SetData(root, 0, i + 1); ASSERT(r); // 노드에 데이터 삽입
}
printf("0번위치에 10개 삽입\n");
disp(root);
//삽입확인
for (int i = 0 ; i < 10 ; i++)
{
r = GetData(root, i, &n); ASSERT(r);
ASSERT(n == 10 - i);
}
printf("0번위치에 10개 삽입확인\n");
disp(root);
//모두삭제
for (int i = 0; i < 10; i++)
{
r = Delete(root, 0); ASSERT(r);
}
printf("모두삭제\n");
disp(root);
// 마지막위치에 10개삽입
for (int i = 0; i < 10; i++)
{
r = Insert(root, i); ASSERT(r);
r = SetData(root, i, i + 1); ASSERT(r);
}
printf("마지막위치에 10개 삽입\n");
disp(root);
// 짝수번째모두삭제
for (int i = 10 - 1; i >= 0; i -= 2)
{
r = Delete(root, i); ASSERT(r);
}
printf("짝수번째 모두 삭제\n");
disp(root);
// 짝수번째다시삽입
for (int i = 1; i < 10; i += 2)
{
r = Insert(root, i); ASSERT(r);
r = SetData(root, i, i + 1); ASSERT(r);
}
printf("짝수번째 다시 삽입\n");
disp(root);
// 복원확인
for (int i = 0; i < 10; i++)
{
r = GetData(root, i, &n); ASSERT(r);
ASSERT(n == i + 1);
}
printf("복원확인\n");
disp(root);
printf("에러확인\n");
for (int i = 10; i < 20; i++)
{
r = GetData(root, i, &n); ASSERT(r);
}
// 역순으로삭제
for (int i = 10 - 1; i >= 0; i--)
{
r = Delete(root, i); ASSERT(r);
}
printf("역순으로 삭제\n");
disp(root);
ASSERT(root->next == NULL);
delete root;
root = NULL; // 좋은습관
}
int Insert(struct Node*root, int position)
{
Node*cur = root; // cur가 root에 위치
for (int i = 0; i < position; i++) // position이 0일때 작동안함
{
if (cur == NULL) return 0; // 생성한 노드 수를 넘을 때 예외처리
cur = cur->next; // position만큼 cur의 위치이동(1부터시작)
}
Node*new1 = (Node*)malloc(sizeof(Node)); // new1생성
new1->next = NULL;
// new1생성 후 연결작업
if (cur->next == NULL) cur->next = new1; // 맨처음 노드 연결 ,마지막의 10개의 노드를 연결시킴
else
{
new1->next = cur->next; // 0번째 10개의 노드 삽입
cur->next = new1;
}
return 1;
}
int Delete(struct Node*root, int position)
{
Node* cur = root;
Node* del;
for (int i = 0; i < position; i++) {
if (cur->next == NULL) return 0;
cur = cur->next;
}
/*
if (cur->next->next == NULL) // 삭제시 del가 가르키는 노드를 임시생성 후 폐기
{
Node*new1 = NULL;
del = cur->next;
del->next = new1;
cur->next = del->next;
del->next = NULL;
free(del);
free(new1);
}
else {
*/
del = cur->next;
cur->next = cur->next->next; // 마지막 노드 삭제 시 어짜피 cur->next->next는 NULL이니까 새로운 노드 만들어서 연결시키지 않아도 됨
del->next = NULL;
free(del);
// }
return 1;
}
int SetData(struct Node*root, int position, int data) {
Node*cur = root;
for (int i = 0; i < position; i++)
{
if (cur->next == NULL) return 0;
cur = cur->next;
}
cur->next->data = data;
return 1;
}
int GetData(struct Node*root, int position, int* data) {
Node*cur = root;
for (int i = 0; i <position; i++) {
if (cur->next == NULL||cur->data==NULL)return 0;
cur = cur->next;
}
if (cur->next == NULL)return 0; // 커서가 가르키는 다음 노드가 없을시 error
*data = cur->next->data;
return 1;
}
void disp(struct Node *root)
{
Node *cur;
cur = root->next;
while (cur!= NULL)
{
printf("result : %d\n", cur->data);
cur = cur->next;
}
if (root->next == NULL)puts("NULL\n"); // new1노드 모두 삭제 시 출력
}