이중 원형 연결 리스트
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <conio.h>
# include <Windows.h>
# define NEW_NODE_ONE 1
# define START_NODE_ZERO 0
typedef struct circle
{
int element;
struct circle* Left;
struct circle* Right;
}circle, *ptrCircle;
typedef struct node
{
ptrCircle headNode;
ptrCircle tailNode;
int nData; // 데이터 갯수
}node, *ptrNode;
// function prototype
void* creatNode(int);
void insertNode(node**);
void forword_PrintF(node**);
void rear_PrintF(node**);
int main(void)
{
ptrNode MyCircle = NULL;
MyCircle = creatNode(START_NODE_ZERO);
MyCircle->headNode = creatNode(NEW_NODE_ONE);
MyCircle->tailNode = creatNode(NEW_NODE_ONE);
insertNode(&MyCircle);
insertNode(&MyCircle);
insertNode(&MyCircle);
insertNode(&MyCircle);
insertNode(&MyCircle);
//forword_PrintF(&MyCircle);
rear_PrintF(&MyCircle);
return 0;
} // end of main function
void* creatNode(int choice)
{
if (choice == NEW_NODE_ONE)
{
ptrCircle newNode = malloc(sizeof(circle));
memset(newNode, 0, sizeof(circle));
return newNode;
}
if (choice == START_NODE_ZERO)
{
ptrNode startNode = malloc(sizeof(node));
memset(startNode, 0, sizeof(node));
return startNode;
}
} // end of creatNode function
void insertNode(node** n)
{
ptrCircle newNode = NULL;
newNode = creatNode(NEW_NODE_ONE);
printf("element input : ");
scanf("%d", &(newNode->element));
//
if ((*n)->nData == 0)
{
printf("test1 \n");
(** n).headNode->Right = newNode; // 1
newNode->Right = (** n).headNode->Right; // 2
(** n).tailNode->Left = newNode; // 3
newNode->Left = (** n).tailNode->Left; // 4
++(** n).nData;
printf("test2 \n");
return; // _End
}
newNode->Left = (** n).tailNode->Left; // 1
newNode->Right = (** n).headNode->Right; // 2
(** n).tailNode->Left->Right = newNode; // 3
(** n).tailNode->Left = newNode; // 4
(** n).headNode->Right->Left = (** n).tailNode->Left; // 5
++(** n).nData;
return; // _End
} // end of insertNode function
void forword_PrintF(node** n)
{
if ((**n).nData == 0)
{
printf("data is empty ... !!! \n");
return;
}
else
{
char Br;
ptrCircle IndexNode = NULL;
IndexNode = (**n).headNode->Right;
while (1)
{
printf("%d\n", IndexNode->element);
if ((Br = getch()) == 'b')
{
break;
}
else
{
IndexNode = IndexNode->Right;
}
}
}
} // end of forword_PrintF function
void rear_PrintF(node** n)
{
if ((**n).nData == 0)
{
printf("data is empty ... !!! \n");
return;
}
else
{
char Br;
ptrCircle IndexNode = NULL;
IndexNode = (**n).tailNode->Left;
while (1)
{
printf("%d\n", IndexNode->element);
if ((Br = getch()) == 'b')
{
break;
}
else
{
IndexNode = IndexNode->Left;
}
}
}
}
'언어 > c언어' 카테고리의 다른 글
합, 평균, 최댓값, 최솟값 (0) | 2017.04.05 |
---|---|
자료구조 덱 (0) | 2017.04.02 |
회문 (0) | 2017.02.09 |
baeckJ 10818 (0) | 2017.01.22 |
beakJ (0) | 2017.01.21 |