#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int element;
typedef struct ListNode{
element data;
struct ListNode* before;
struct ListNode* next;
}ListNode;
typedef struct {
ListNode* head; //헤드 포인터
int length; //노드의 개수
}LinkedListType;
void error(char* sentence){
fprintf(stderr, "%s\n", sentence);
exit(1);
}
ListNode* createNode(element data){
ListNode* newNode = (ListNode*)NULL;
newNode = (ListNode*)malloc(sizeof(ListNode));
if (newNode == (ListNode*)NULL)
error("MEMORY ALLOCATION FAILED");
else
newNode->data = data;
return newNode;
}
void init(LinkedListType* list){
list->head = createNode((element)NULL);
list->length = 0;
list->head->before = list->head;
list->head->next = list->head;
}
int getLength(LinkedListType* list){ //리스트의 항목의 개수를 반환
return list->length;
}
int isEmpty(LinkedListType* list){ //리스트가 비었는지 검사
return (getLength(list) == 0) ? TRUE : FALSE;
}
void insertListNode(LinkedListType* list, ListNode* newNode){
newNode->before = list->head;
newNode->next = list->head->next;
list->head->next->before = newNode;
list->head->next = newNode;
list->length++;
}
void display(LinkedListType* list){
ListNode* p;
if (isEmpty(list)) return;
for (p = list->head->next; p != list->head; p = p->next)
printf("%d->", p->data);
printf("\n");
}
void displayReverse(LinkedListType* list){
ListNode* p;
if (isEmpty(list)) error("list is Empty");
for (p = list->head->before; p != list->head; p = p->before)
printf("%d->", p->data);
printf("\n");
}
void removeNode(LinkedListType* list, ListNode* removed){
ListNode* p = list->head;
if (removed == p) return;
removed->before->next = removed->next;
removed->next->before = removed->before;
list->length--;
free(removed);
}
ListNode* getNodeAt(LinkedListType* list, int position){ //리스트안에서 pos위치의 노드를 반환
int countPos = 0;
ListNode* p;
for (p = list->head->next; p != list->head; p = p->next){
if (++countPos == position)
return (ListNode*)p;
}
return (ListNode*)NULL;
}
void add(LinkedListType* list, int position, element data){ //주어진 위치에 데이터 삽입
LinkedListType temp;
int positionInit = position - 1;
if (positionInit < 0){
error("Bound of Position Exception\n");
}
else if (positionInit == 0){
insertListNode(list, createNode(data));
}
else{
temp.head = getNodeAt(list, positionInit);
insertListNode(&temp, createNode(data));
list->length++;
}
}
void addLast(LinkedListType* list, element data) { //리스트의 끝에 데이터를 삽입
add(list, getLength(list), data);
}
void addFirst(LinkedListType* list, element data) { //리스트의 처음에 데이터 삽입
add(list, 1, data);
}
void Delete(LinkedListType* list, int position) { //주어진 위치의 데이터를 삭제
if (position < 0) {
error("Bound of Position Exception\n");
}
else if (position == 0) {
error("Bound of Position Exception <<Zero is HeadPoint>> \n");
}
else {
removeNode(list, getNodeAt(list, position));
}
}
element getEntry(LinkedListType* list, int position){
if (position < 0) {
error("Bound of Position Exception\n");
}
else if (position == 0) {
error("Bound of Position Exception <<Zero is HeadPoint>> \n");
}
else {
return (element)getNodeAt(list, position)->data;
}
return (element)NULL;
}
void clear(LinkedListType* list) {
int i = 0;
int countPos = getLength(list);
for (; i < countPos; i++)
removeNode(list, list->head->next);
}
element isInList(LinkedListType * list, element item) { //데이터 값이 item인 노드를 찾는다
ListNode *p;
for (p = list->head->next; p != list->head; p = p->next){
if (p->data == item){
return p->data;
}
}
printf("Do not Find element\n");
return (element)NULL;
}
int main(void){
LinkedListType list;
ListNode* temp;
element retTemp;
int i;
init(&list);
//display(&list);
insertListNode(&list, createNode(10));
insertListNode(&list, createNode(20));
insertListNode(&list, createNode(30));
display(&list);
displayReverse(&list);
//removeNode(&list, list.head->next);
//display(&list);
temp = getNodeAt(&list, 3);
printf("%d\n", temp->data);
printf("항목의 개수:%d\n", getLength(&list));
add(&list, 3, 999); // 3번 자리에 노드 삽입
display(&list);
addFirst(&list, 9999); // 노드 처음에 삽입
display(&list);
Delete(&list, 0); // 5번째 노드 제거
display(&list);
for (i = 1; i <= getLength(&list); i++){
// 노드 순서대로 1, 2, 3, 4
retTemp = getEntry(&list, i); // next 기준으로 i번째 노드의 element 반환
printf("getEntry : %d\n", retTemp);
}
printf("%d\n", getLength(&list));
retTemp = 0;
retTemp = isInList(&list, 588);
printf("Isinlist '588'? : %d\n", retTemp);
clear(&list);
display(&list);
return 0;
}
'이전것 > Algorithm' 카테고리의 다른 글
Delegate Memorial Code (0) | 2015.05.26 |
---|---|
큐 랜덤 넣고 빼고 (0) | 2015.05.13 |
실습과제#2 (0) | 2014.09.26 |
Insertion_Sort (0) | 2014.09.23 |
Selection Sort (0) | 2014.09.23 |