순환 링크 구현
16779 단어 엄 울 민 데이터 구조 실현
#include
#include
typedef struct Node
{
int data;
struct Node * next;
}Node, *LinkList;
LinkList Creat();
void Destroy(LinkList *L);
void Insert(LinkList *L, int val, int index);
void Delete(LinkList *L, int index);
void Traverse(LinkList L);
int main()
{
LinkList L = Creat();
Traverse(L);
Insert(&L, 1, 5);
printf("After inserting is :
");
Traverse(L);
printf("After deleting is :
");
Delete(&L, 2);
Traverse(L);
Destroy(&L);
Traverse(L);
}
LinkList Creat()
{
LinkList L = (LinkList)malloc(sizeof(Node));// L , L
int n;
L->data = -1;
L->next = L;// , ! 。
printf("Please enter the number you want input:(>5)");
scanf_s("%d", &n);
printf("input the number:
");
for (int i = 0; i < n; i++)
{
LinkList p = (LinkList)malloc(sizeof(Node));
scanf_s("%d", &p->data);
p->next = L->next;
L->next = p;
L = p;
}
return L;//
}
void Destroy(LinkList *L)
{
LinkList q = (*L)->next->next;
LinkList p;
(*L) = (*L)->next;
while (q != (*L))
{
p = q->next;
free(q);
q = p;
}
(*L)->next = (*L);
}
void Insert(LinkList *L, int val, int index)
{
LinkList p = (LinkList)malloc(sizeof(Node));
p->data = val;
LinkList q = (*L)->next;
for (int i = 1; q != (*L) && i < index; i++)
q = q->next;
p->next = q->next;
q->next = p;
if (p == (*L))
(*L) = p;
}
void Delete(LinkList *L, int index)
{
LinkList q = (*L)->next, p;
for (int i = 0; i < index; i++)
q = q->next;
p = q->next;
q->next = p->next;
free(p);
}
void Traverse(LinkList L)
{
LinkList q = L->next->next;//
while (q != L)
{
printf("%d->", q->data);
q = q->next;
}
printf("NULL
");
}