循环单链表
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#define random(x) rand() % (x)
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
}*LNode;
typedef struct LinkList
{
struct LNode* head;
int size;
}LinkList;
int visit(void* data)
{
printf("%d\t",*(int*)data);
return 1;
}
Status InitLoopSingleList(LinkList* List);
Status CreateLoopSingleList(LinkList* List,int n);
void print(LinkList List,int visit());
Status LoopLinkListReverse1(LinkList* List);
Status BubbleSort(LinkList* List);
Status Insert(LinkList* List,int i,ElemType e);
Status Delete(LinkList* List,int i,ElemType* e);
int main()
{
LinkList List;
int n = 0;
if(InitLoopSingleList(&List) == 0)
exit(1);
printf("please input the counter of number\n");
scanf("%d",&n);
CreateLoopSingleList(&List,n);
print(List,&visit);
LoopLinkListReverse1(&List);
print(List,&visit);
Insert(&List,4,0);
print(List,&visit);
BubbleSort(&List);
print(List,&visit);
return 0;
}
Status Delete(LinkList* List,int i,ElemType* e)
{
assert(i >= 1 && i <= List -> size);
LNode head = List -> head,p = head,q = NULL;
int j = 1;
while(p && j < i)
{
p = p -> next;
j++;
}
q = p -> next;
*e = q -> data;
p -> next = q -> next;
free(q);
List -> size--;
return 1;
}
Status Insert(LinkList* List,int i,ElemType e)
{
assert(i >= 1 && i <= List -> size + 1);
LNode head = List -> head,p = head,pnew = NULL;
int j = 1;
while(p && j < i)
{
p = p -> next;
j++;
}
if((pnew = (LNode) malloc (sizeof(LNode))) == 0)
return 0;
pnew -> data = e;
pnew -> next = p -> next;
p -> next = pnew;
List -> size++;
return 1;
}
Status LoopLinkListReverse1(LinkList* List)
{
assert(List -> head != NULL);
if(List -> size == 1)
return 1;
LNode head = List -> head,p = head -> next -> next,q = NULL;
head -> next -> next = head;
while(p != head)
{
q = p;
p = p -> next;
q -> next = head -> next;
head -> next = q;
}
return 1;
}
Status BubbleSort(LinkList* List)
{
assert(List -> head != NULL);
if(List -> size < 2)
return 1;
LNode head = List -> head,p = NULL,q = NULL,ptail = head;
while(head -> next != ptail)
{
q = head;
p = q -> next -> next;
while(p != ptail)
{
if(q -> next -> data > p -> data)
{
q -> next -> next = p -> next;
p -> next = q -> next;
q -> next = p;
}
q = q -> next;
p = q -> next -> next;
}
ptail = q -> next;
}
return 1;
}
void print(LinkList List,int visit())
{
LNode p = List.head -> next;
while(p != List.head)
{
visit(&p -> data);
p = p -> next;
}
printf("\n");
}
Status CreateLoopSingleList(LinkList* List,int n)
{
LNode p = List -> head,pnew = NULL;
int i = 0;
srand(time(NULL));
while(i < n)
{
if((pnew = (LNode) malloc (sizeof(struct LNode))) == 0)
return 0;
pnew -> data = random(100);
pnew -> next = p -> next;
p -> next = pnew;
p = pnew;
i++;
}
p -> next = List -> head;
List -> size += n;
return 1;
}
Status InitLoopSingleList(LinkList* List)
{
LNode head;
if((head = (LNode) malloc (sizeof(struct LNode))) == 0)
return 0;
head -> next = head;
List -> head = head;
List -> size = 0;
return 1;
}