상세 컨텐츠

본문 제목

C 언어 예제, 연결 리스트 연산, 데이터 자유자재로 추가 제거

컴퓨터 관련/C 언어 예제

by 열정과 함께 2015. 1. 8. 20:14

본문

구조체하고 노드 공부하다가 안 사실인데 이렇게 앞뒤로 이어진 데이터를 연결 리스트라고 부른다더라. 그래서 이렇게 제목을 바꾸었다.


아래 코딩은 이전과 비교해서 다음의 특징을 좀 넣어봤다.


1) 뒤쪽에 있는 데이터를 가리키는 노드 값 말고 앞쪽에 있는 데이터도 가리키도록 했다.

2) 맨 뒤의 데이터와 맨 앞의 데이터가 연결되도록 했다.


아래의 코드는 다음과 같은 함수로 이루어져 있다.


first_node

insert_node

delete_node


연결 리스트 연산을 하려니 연결을 하는 구조를 만들어야 될 텐데 그럼 맨 처음 들어가는 데이터는 연결을 할 거리가 없지 않은가. 그래서 예외를 두고 first_node 함수로 처리하였다.


그리고 insert_node 와 delete_node 함수인데, 각각 Data No.1 으로부터의 거리를 계산해서 넣고 지우도록 했다. 그러니까 삽입을 넣고 1을 입력하면 Data No.1 이 새로 생기는 것이 아니라, Data No.1 으로 부터 첫 번째에 있는, 그러니까 Data No.1 과 Data No. 2 사이에 있는 공간에 데이터가 들어가게 된다. 이를테면 Data No. 2 가 새로 생기는 것이다. delete_node 도 마찬가지의 방식으로 작동한다.


만약에 데이터는 3개만 있는데 삽입 위치를 15, 16 쯤으로 하면 어떤가? 그러면 Data No. 1 의 뒤를 1번째로 시작해서, 계속해서 2, 3, 4, .... 15 까지 센 다음에 그 위치에 데이터를 넣게 된다. 단 이 때, 맨 뒤의 데이터는 맨 앞과 연결되어 있기 때문에 하나의 공간으로 생각하여 한 번만 센다.





 

코드는 아래와 같다.


#include <stdio.h>

#include <stdlib.h>

/* 노드를 새로 할당 / 노드를 원하는 위치에 삽입 / 순환하는 노드 / 앞에도 노드 / 노드 제거 기능 / 기준점 Node_arr[0] */

/* make_node 함수 수정해야 함. */

struct Node

{

int data;

struct Node *nextNode;

struct Node *prevNode;

};

struct Node first_node(struct Node *Node_arr, int *node_data, struct Node *marker_node);

struct Node insert_node(int *insert_location, struct Node *marker_node);

struct Node delete_node(int *delete_location, struct Node *marker_node);

int node_check(struct Node *marker_node);

int main()

{

int node_data, i;

int insert_location, delete_location;

struct Node *Node_arr;

struct Node *marker_node;

printf("첫 번째 데이터를 입력합니다. \n입력하려는 데이터 값은 얼마입니까? : ");

scanf("%d", &node_data);

Node_arr=(struct Node *)malloc(sizeof(struct Node));

marker_node=Node_arr;

first_node(Node_arr, &node_data, marker_node);

node_check(marker_node);

for(;;)

{

printf("수행하려는 작업은 무엇입니까? 기준점은 Data No.1 입니다. \n 1. 데이터 삽입\n 2. 데이터 제거 \n 3. 데이터 조회\n 4. 종료  : ");

scanf("%d", &i);

if(i==1)

{

printf("삽입하려는 위치를 입력하십시오. 음수는 입력 불가합니다. : ");

scanf("%d", &insert_location);

insert_node(&insert_location, marker_node);

node_check(marker_node);

}

else if(i==2)

{

printf("지우려는 데이터를 입력하십시오. 음수는 입력할 수 없습니다.");

scanf("%d", &delete_location);

delete_node(&delete_location, marker_node);

node_check(marker_node);

}

else if(i==3)

{

node_check(marker_node);

}

else if(i==4)

{

printf("연산이 종료됩니다. \n");

break;

}

else

{

printf("잘못된 연산입니다. \n");

}


}


return 0;

}

int node_check(struct Node *marker_node)

{

int i=0;

struct Node *location;

location=marker_node;

printf("데이터 값을 점검합니다. \n");

for(;;)

{

printf("Data No.%d = %d \n", i+1,location->data);

i++;

if(location->nextNode==marker_node)

{

break;

}

else

{

location=location->nextNode;

}

}

printf("데이터 값 점검 끝 \n");

return 0;

}

struct Node first_node(struct Node *Node_arr, int *node_data, struct Node *marker_node)

{

int i;

Node_arr->data=*node_data;

Node_arr->nextNode=Node_arr;

Node_arr->prevNode=Node_arr;

return *marker_node;

}

struct Node insert_node(int *insert_location, struct Node *marker_node)

{

int data_add;

int i=0;

struct Node *new_location;

struct Node *temp_location;

printf("추가하려는 변수 data 의 값은 얼마입니까? : ");

scanf("%d", &data_add);

new_location=(struct Node *)malloc(sizeof(struct Node));

new_location->data=data_add;

if(*insert_location>0)

{

temp_location=marker_node;

for(i=0;i<*insert_location;i++)

{

temp_location=temp_location->nextNode;

}

new_location->nextNode=temp_location;

new_location->prevNode=temp_location->prevNode;

(temp_location->prevNode)->nextNode=new_location;

temp_location->prevNode=new_location;

}

else if(*insert_location==0)

{

temp_location=marker_node;

marker_node=new_location;

new_location->nextNode=temp_location;

new_location->prevNode=temp_location->prevNode;

(temp_location->prevNode)->nextNode=new_location;

temp_location->prevNode=new_location;

}

else

{

printf("음수는 입력할 수 없습니다. \n");

free(new_location);

}

return *marker_node;

}

struct Node delete_node(int *delete_location, struct Node *marker_node)

{

struct Node *temp_location1, *temp_location2;

int i;

temp_location1=marker_node->nextNode;

temp_location2=marker_node->prevNode;

if(delete_location<0)

{

printf("음수는 입력할 수 없습니다. \n");

}

else if(delete_location==0)

{

free(marker_node);

marker_node=temp_location1;

temp_location1->prevNode=marker_node;

temp_location2->nextNode=marker_node;

}

else

{

for(i=0;i<*delete_location-1;i++)

{

temp_location1=temp_location1->nextNode;

}

temp_location2=temp_location1->prevNode;

temp_location2->nextNode=temp_location1->nextNode;

temp_location2=temp_location1->nextNode;

temp_location2->prevNode=temp_location1->prevNode;

free(temp_location1);

}

return *marker_node;

}






관련글 더보기

댓글 영역