1. 단어들을 저장하는 연결 리스트 만들기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char name[100];
} element;
typedef struct ListNode{
element data;
struct ListNode *link;
} ListNode;
void error(char *message)
{
fprintf(stderr, "%s\n", message);
exit(1);
}
ListNode* insert_first(ListNode *head, element value)
{
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
p->data = value;
p->link = head;
head = p;
return head;
}
void print_list(ListNode *head)
{
for(ListNode *p = head; p != NULL; p = p->link)
pirntf("%s->", p->data.name);
printf("NULL \n");
}
int main(void)
{
ListNode *head = NULL;
element data;
strcpy(data.name, "APPLE");
head = insert_first(head, data);
print_list(head);
strcpy(data.name, "KIWI");
head = insert_first(head, data);
print_list(head);
strcpy(data.name, "BANANA");
head = insert_first(head, data);
print_list(head);
return 0;
}
2. 특정한 값을 탐색하는 함수
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode{
element data;
struct ListNode *link;
} ListNode;
ListNode* insert_first(ListNode *head, element value)
{
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
p->data = value;
p->link = head;
head = p;
return head;
}
void print_list(ListNode *head)
{
for(ListNode *p = head; p != NULL; p = p->link)
printf("%d->", p->data);
printf("NULL \n");
}
ListNode* search_list(ListNode *head, element x)
{
ListNode *p = head;
while (p != NULL){
if(p->data == x) return p;
p = p->link;
}
return NULL;
}
int main(void)
{
ListNode *head = NULL;
head = insert_first(head, 10);
print_list(head);
head = insert_first(head, 20);
print_list(head);
head = insert_first(head, 30);
print_list(head);
if(search_list(head, 30) != NULL)
printf("리스트에서 30을 찾았습니다. \n");
else
printf("리스트에서 30을 찾지 못했습니다. \n");
return 0;
}
3. 두개의 리스트를 하나로 합치는 함수
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode{
element data;
struct ListNode *link;
} ListNode;
ListNode* insert_first(ListNode *head, element value)
{
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
p -> data = value;
p -> link = head;
head = p;
return head;
}
void print_list(ListNode *head)
{
for (ListNode *p = head; p != NULL; p = p->link)
printf("%d->", p->data);
printf("NULL \n");
}
ListNode* concat_list(ListNode *head1, ListNode *head2)
{
if(head1 == NULL) return head2;
else if(head2 == NULL) return head1;
else{
ListNode *p;
p = head1;
while(p->link != NULL)
p = p->link;
p->link = head2;
return head1;
}
}
int main(void)
{
ListNode* head1 = NULL;
ListNode* head2 = NULL;
head1 = insert_first(head1, 10);
head1 = insert_first(head1, 20);
head1 = insert_first(head1, 30);
print_list(head);
head2 = insert_first(head1, 40);
head2 = insert_first(head1, 50);
print_list(head2);
ListNode *total = concat_list(head1, head2);
print_list(total);
return 0;
}
4. 리스트를 역순으로 만드는 연산
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode{
element data;
struct ListNode *link;
} ListNode;
ListNode* insert_first(ListNode *head, element value)
{
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
p->data = value;
p->link = head;
head = p;
return head;
}
void print_list(ListNode *head)
{
for(ListNode *p = head; p != NULL; p = p->link)
printf("%d->", p->data);
printf("NULL \n");
}
ListNode* reverse(ListNode *head)
{
ListNode *p, *q, *r;
p = head;
q = NULL;
while(p != NULL){
r = q;
q = p;
p = p->link;
q->link = r;
}
return q;
}
int main(void)
{
ListNode* head1 = NULL;
ListNode* head2 = NULL;
head1 = insert_first(head1, 10);
head1 = insert_first(head1, 20);
head1 = insert_first(head1, 30);
print_list(head1);
head2 = reverse(head1);
print_list(head2);
return 0;
}