printf("ho_tari\n");
과제 - 원형 연결리스트 구현 본문
#include <stdio.h>
#include<stdlib.h>
typedef struct cnode
{
int data;
struct cnode* next;
}CNODE;
int getCLlength(CNODE* head)
{
struct cnode* cur = head;
int count = 0;
if (head == NULL)
{
printf("[+]CL length: 0\n");
return 0;
}
else
{
cur = cur->next;
count++;
while (cur != head)
{
cur = cur->next;
count++;
}
printf("[+]CL length:%d\n", count);
}
}
void printCL(CNODE* head)
{
CNODE* p;
if (head == NULL)
{
printf("[-]CList is empty\n");
return;
}
else
{
printf("[+]");
p = head;
printf("%d ", p->data);
p = p->next;
while (p != head)
{
printf("%d ", p->data);
p = p->next;
}
}
printf("\n");
}
void insertEnd(CNODE** head, int a)
{
printf("input a new data:");
scanf("%d", &a);
CNODE* q = (CNODE*)malloc(sizeof(CNODE));
q->data = a;
if (*head == NULL)
{
*head = q;
q->next = *head;
}
else
{
CNODE* t = *head;
while (t->next != *head)
{
t = t->next;
}
t->next = q;
q->next = *head;
}
}
void insertBegin(CNODE** head, int a)
{
printf("input a new data: ");
scanf("%d", &a);
CNODE* q = (CNODE*)malloc(sizeof(CNODE));
q->data = a;
if (*head == NULL)
{
*head = q;
q->next = *head;
}
else
{
CNODE* t = *head;
while (t->next != *head)
{
t = t->next;
}
q->next = *head;
*head = q;
t->next = *head;
}
}
void delNode(CNODE** head)
{
if (*head == NULL)
{
printf("[-]CList is empty.\n");
return;
}
else
{
CNODE* t = *head;
CNODE* t_pr = NULL;
while (t->next != *head)
{
t_pr = t;
t = t->next;
}
CNODE* r = t;
if (t == *head)
{
*head = NULL;
}
else
{
t_pr->next = *head;
}
free(r);
}
}
int main()
{
int num = 0;
CNODE* head = 0;
int a = 0;
while (num != 6)
{
printf("1.length 2.print 3.add last 4.add begin 5.delete 6.exit\n");
scanf("%d", &num);
switch (num)
{
case 1:
getCLlength(head);
break;
case 2:
printCL(head);
break;
case 3:
insertEnd(&head, a);
break;
case 4:
insertBegin(&head, a);
break;
case 5:
delNode(&head);
break;
case 6:
printf("[-]Terminate...\n");
break;
default:
break;
}
}
}
'대학교 2학년 1학기 > 데이터구조' 카테고리의 다른 글
2주차 과제 (0) | 2023.09.04 |
---|---|
1주차 과제 (0) | 2023.09.04 |
과제 - 단순 연결리스트 구현 (0) | 2023.09.04 |
과제 - 원형 큐 구현 (0) | 2023.09.04 |
과제 - 함수 포인터를 이용한 계산기 구현 (0) | 2023.09.04 |