728x90
반응형
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct _node
{
	int data;
	struct _node* pre;
	struct _node* next;
}Node;

typedef struct _list
{
	Node* head;
	Node* curr;
	int number;
}List;

void Init(List* list)
{
	list->head = NULL;
	list->number = 0;
}

void Insert(List* list, int data)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = data;
	
	newNode->next = list->head;
	if (list->head != NULL)
	{
		list->head->pre = newNode;
	}
	newNode->pre = NULL;
	list->head = newNode;

	(list->number)++;
}

int Count(List* list)
{
	return list->number;
}

int First(List* list, int* pdata)
{
	if (list->head == NULL)
	{
		return FALSE;
	}
	list->curr = list->head;
	*pdata = list->curr->data;

	return TRUE;
}

int Next(List* list, int* pdata)
{
	if (list->curr->next == NULL)
	{
		return FALSE;
	}
	list->curr = list->curr->next;
	*pdata = list->curr->data;

	return TRUE;
}

int Previous(List* list, int* pdata)
{
	if (list->curr->pre == NULL)
	{
		return FALSE;
	}
	list->curr = list->curr->pre;
	*pdata = list->curr->data;

	return TRUE;
}

int main(void)
{
	List list;
	int data;
	Init(&list);

	Insert(&list, 10);
	Insert(&list, 20);
	Insert(&list, 30);
	Insert(&list, 20);
	Insert(&list, 40);
	Insert(&list, 50);

	printf("Current Number of Data: %d\n", Count(&list));

	if (First(&list, &data))
	{
		printf("%d ", data);
		
		while (Next(&list, &data))
		{
			printf("%d ", data);
		}

		while (Previous(&list, &data))
		{
			printf("%d ", data);
		}

		printf("\n\n");
	}

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Stack write myself  (0) 2022.10.21
Maze.py  (0) 2022.10.21
Circular Linked List(+ Merger)  (0) 2022.10.20
Bidirectional Linked List(+ division)  (0) 2022.10.20
Circular Linked List(+ division)  (0) 2022.10.20

+ Recent posts