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* head;
Node* curr;
Node* back;
int number;
int (*comp)(int x, int y);
} List;
void Init(List* copy)
{
copy->head = (Node*)malloc(sizeof(Node));
copy->head->next = NULL;
copy->comp = NULL;
copy->number = 0;
}
void FInsert(List* copy, int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = copy->head->next;
copy->head->next =newNode;
(copy->number)++;
}
void SInsert(List* copy, int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
Node* pred = copy->head;
newNode->data = data;
while(pred -> next != NULL &&
copy->comp(data, pred->next->data) != 0)
{
pred = pred->next;
}
newNode -> next = pred -> next;
pred -> next = newNode;
(copy->number)++;
}
void Insert(List* copy, int data)
{
if (copy->comp == NULL)
FInsert(copy, data);
else
SInsert(copy, data);
}
int First(List* copy, int* pdata)
{
if(copy->head->next == NULL)
return FALSE;
copy->back = copy->head;
copy->curr = copy->head->next;
*pdata = copy->curr->data;
return TRUE;
}
int Next(List* copy, int* pdata)
{
if(copy->curr->next == NULL)
return FALSE;
copy->back = copy->curr;
copy->curr = copy->curr->next;
*pdata = copy->curr->data;
return TRUE;
}
int Remove(List* copy)
{
Node* rpos = copy->curr;
int rdata = rpos->data;
copy->back->next = copy->curr->next;
copy->curr = copy->back;
free(rpos);
(copy->number)--;
return rdata;
}
int Count(List* copy)
{
return copy->number;
}
void SetSortRule(List* copy, int (*comp)(int x, int y))
{
copy->comp = comp;
}
int main(void)
{
List list;
int data;
Init(&list);
Insert(&list, 10);
Insert(&list, 20);
Insert(&list, 30);
Insert(&list, 40);
Insert(&list, 50);
Insert(&list, 30);
printf("Current Number of Data: %d\n", Count(&list));
if(First(&list, &data)) // Search for First data
{
printf("%d ", data);
while(Next(&list, &data)) // Search to after second data
printf("%d ", data);
}
printf("\n\n");
// Search and Remove Number 30
if(First(&list, &data))
{
if(data == 30)
Remove(&list);
while(Next(&list, &data))
{
if(data == 30)
Remove(&list);
}
}
// After removed then Print entire data
printf("Current Number of data: %d \n", Count(&list));
if(First(&list, &data))
{
printf("%d ", data);
while(Next(&list, &data))
printf("%d ", data);
}
printf("\n\n");
return 0;
}
/*
Current Number of Data: 6
30 50 40 30 20 10
Current Number of data: 4
50 40 20 10
*/
728x90
반응형
'Data Structure & Algorithm' 카테고리의 다른 글
Bidirectional Linked List(+ division) (0) | 2022.10.20 |
---|---|
Circular Linked List(+ division) (0) | 2022.10.20 |
[practice] sequential (0) | 2022.10.19 |
Maze (0) | 2022.10.19 |
other bill (0) | 2022.10.19 |