728x90
반응형
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

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

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

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

void FrontInsert(List* list, int data)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = data;

	if (list->tail == NULL)
	{
		list->tail = newNode;
		newNode->next = newNode;
	}
	else
	{
		newNode->next = list->tail->next;
		list->tail->next = newNode;
	}
	(list->number)++;
}

void Insert(List* list, int data)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = data;

	if (list->tail == NULL)
	{
		list->tail = newNode;
		newNode->next = newNode;
	}
	else
	{
		newNode->next = list->tail->next;
	}
	(list->number)++;
}

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

	*pdata = list->curr->data;
	return TRUE;
}

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

	*pdata = list->curr->data;
	return TRUE;
}

int Remove(List* list)
{
	Node* temp = list->curr;
	int temp_data = temp->data;

	if (temp == list->tail)
	{
		if (list->tail == list->tail->next)
		{
			list->tail = NULL;
		}
		else
		{
			list->tail = list->back;
		}
	}

	list->back->next = list->curr->next;
	list->curr = list->back;

	free(temp);
	(list->number)--;
	return temp_data;
}


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

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

	FrontInsert(&list, 10);
	FrontInsert(&list, 20);
	FrontInsert(&list, 30);
	Insert(&list, 40);
	Insert(&list, 20);
	Insert(&list, 60);

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

	if (First(&list, &data))
	{
		printf("%d ", data);
		
		for (i = 0; i < 6; i++)
		{
			if (Next(&list, &data))
			{
				printf("%d ", data);
			}
		}
		
		/* Caution: Infinite Loop *//*
		while (Next(&list, &data))
		{
			printf("%d ", data);
		}
		*/
	}
	printf("\n\n");

	// 2의 배수 찾아 모두 삭제 //

	nodeNum = Count(&list);

	if(nodeNum != 0)
	{
		First(&list, &data);
		if (data % 2 == 0)
		{
			Remove(&list);
		}
		for (i = 0; i < nodeNum - 1; i++)
		{
			Next(&list, &data);
			if (data % 2 == 0)
			{
				Remove(&list);
			}
		}
	}

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

	return 0;
}
728x90
반응형

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

Maze.py  (0) 2022.10.21
Bidirectional Linked List(+ Merger)  (0) 2022.10.20
Bidirectional Linked List(+ division)  (0) 2022.10.20
Circular Linked List(+ division)  (0) 2022.10.20
Sequential Dummynode  (0) 2022.10.19

+ Recent posts