#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node * next;
}Node,*link;
void InitList(link * list)
{
(*list) = (link)malloc(sizeof(Node));
if(!(*list))
{
exit(-1);
}
(*list)->data = -9;
(*list)->next = *list;
//printf("+++%d+++\n",(*list)->data);
}
void CreateList(link list)
{
link p;
p = (link)malloc(sizeof(Node));
if(p)
{
printf("Please input the data\n");
scanf("%d",&(p->data));
}
else
{
exit(-1);
}
p->next = list->next;
list->next = p;
//printf("+++%d+++\n",list->next->data);
}
void ClearList(link list)
{
link p = list->next;
link q;
while(p != list)
{
q = p->next;
free(p);
p = q;
}
list->next = list;
}
void PrintList(link list)
{
if(list != NULL)
{
if(list->next != NULL)
{
link p = list->next;
printf("The list is shown below:\n");
while(p != list)
{
printf("---%d---\n",p->data);
p = p->next;
}
}
else
{
printf("The list is empty\n");
}
}
else
{
printf("The list does not exist\n");
}
}
void DestroyList(link *list)
{
link p = *list;
while(p->next != *list)
{
p = p->next;
}
p->next = NULL;
link q;
while(*list)
{
q = (*list)->next;
free(*list);
*list = q;
}
}
int ListEmpty(link list)
{
if(list->next != NULL)
{
printf("The list is not empty\n");
return 0;
}
else
{
printf("The list is empty");
return 1;
}
}
int ListLength(link list)
{
int i = 0;
link p = list->next;
while(p != list)
{
i++;
p = p->next;
}
printf("The length of list is %d\n",i);
return i;
}
int GetElem(link list, int i, int *e)
{
link p = list->next;
int j = 1;
while(p != list && j < i)
{
p = p->next;
j++;
}
if(j>i)
{
return -1;
}
*e = p->data;
//printf("+++The data is %d+++\n",*e);
return 0;
}
int equal(int a, int b)
{
if(a == b)
{
return 1;
}
else
{
return 0;
}
}
int LocateElem(link list, int e, int(*compare)(int a, int b))
{
int i = 1;
link p = list->next;
while(p->next != list)
{
i++;
if(compare(e,p->data))
{
return i;
}
p = p->next;
}
return -1;
}
int PriorElem(link list, int cur, int *pre)
{
link p = list->next;
link q;
while(p != list)
{
q = p->next;
if(q->data == cur)
{
*pre = p->data;
return 0;
}
p = q;
}
return -1;
}
int NextElem(link list, int cur, int *next)
{
link p = list->next;
while(p->next != list)
{
if(p->data == cur)
{
*next = p->next->data;
return 0;
}
p = p->next;
}
return -1;
}
int ListInsert(link list, int i, int e)
{
link p;
p = list;
link tmp;
int j = 0;
while(p->next != list && j < i-1)
{
p = p->next;
j++;
}
if(!p || j > i-1)
{
return -1;
}
tmp = (link)malloc(sizeof(node));
tmp->data = e;
tmp->next = p->next;
p->next = tmp;
return 0;
}
int ListDelete(link list, int i, int *e)
{
int j = 1;
link p = list->next;
while(p != list && j < i-1)
{
p = p->next;
j++;
}
if(!p || j > i-1)
{
return -1;
}
*e = p->next->data;
link q = p->next;
p->next = q->next;
free(q);
return 0;
}
int main()
{
link head;
InitList(&head);
for(int i = 0; i < 3; i++)
CreateList(head);
PrintList(head);
ListEmpty(head);
ListLength(head);
int f = 0;
int choice = 0;
printf("\nplease input the choice\n");
scanf("%d",&choice);
if(choice <= ListLength(head) && choice >= 0)
{
GetElem(head,choice,&f);
printf("The value is %d\n",f);
}
else
{
printf("the choice is out of list length\n");
}
int result = LocateElem(head,3,equal);
printf("\n+++The index is %d\n",result-1);
result = -2;
PriorElem(head,1,&result);
printf("\n@@@result is %d\n",result);
result = -2;
NextElem(head,4,&result);
printf("\n###result is %d\n",result);
printf("\n-----------\n");
result = -2;
ListInsert(head,1,8);
ListInsert(head,1,9);
PrintList(head);
printf("\n-----------\n");
ListDelete(head,3,&result);
PrintList(head);
//ClearList(head);
//PrintList(head);
DestroyList(&head);
PrintList(head);
return 0;
}
带头结点的循环链表基本操作
最新推荐文章于 2023-05-17 14:18:34 发布