#include "circle_list.h"
int create_list(list_t **my_list){
if(NULL==my_list){
return -1;
}
*my_list=(list_t *)malloc(sizeof(list_t));
if(NULL==*my_list){
return -1;
}
(*my_list)->phead=NULL;
return 0;
}
int print_list(list_t *my_list){
if(NULL==my_list)
return -1;
nd_t *ptemp=my_list->phead->next;
//打印头节点
printf("%d ",my_list->phead->data);
//当ptemp==mylist->phead时,说明回到了开头
while(ptemp!=my_list->phead){
printf("%d ",ptemp->data);
ptemp=ptemp->next;
}
putchar(10);
return 0;
}
int insert_list_by_head(list_t *my_list,int num){
//入参不合理
if(NULL==my_list)
return -1;
//创建新节点
nd_t *pnew=(nd_t *)malloc(sizeof(nd_t));
if(NULL==pnew)
return -1;
pnew->data=num;
//插入首节点
if(NULL==my_list->phead){
my_list->phead=pnew;
pnew->next=pnew;
return 0;
}
//如果插入的不是第一个节点
//先找到尾节点
nd_t *ptemp=my_list->phead;
while(ptemp->next!=my_list->phead)
{
ptemp=ptemp->next;
}
//插入
ptemp->next=pnew;
pnew->next=my_list->phead;
my_list->phead=pnew;
return 0;
}
int insert_list_by_tail(list_t *my_list,int num){
if(NULL==my_list)
return -1;
//创建新节点
nd_t *pnew=(nd_t *)malloc(sizeof(nd_t));
if(NULL==pnew)
return -1;
pnew->data=num;
//插入首节点
if(NULL==my_list->phead){
my_list->phead=pnew;
pnew->next=pnew;
return 0;
}
//找到尾节点
nd_t *ptemp=my_list->phead;
while(ptemp->next!=my_list->phead)
{
ptemp=ptemp->next;
}
//插入
ptemp->next=pnew;
pnew->next=my_list->phead;
return 0;
}
int insert_list_by_pos(list_t *my_list,int pos,int num){
if(NULL==my_list)
return -1;
if(pos<0)
return -1;
//找到要插入的节点的前一位
nd_t *ptemp=my_list->phead;
int i=0;//记录ptemp移动了几步
while(ptemp->next!=my_list->phead){
if(i==pos-1) break;
ptemp=ptemp->next;
i++;
}
//如果已经到尾节点了还没移动到pos位置就说明位置不合理
if(i<pos-1){
printf("位置不合理\n");
return -1;
}
//创建新节点
nd_t *pnew=(nd_t *)malloc(sizeof(nd_t));
if(NULL==pnew)
return -1;
pnew->data=num;
//插入第一个节点
if(NULL==my_list->phead){
my_list->phead=pnew;
pnew->next=pnew;
return 0;
}
//插入到头节点
if(0==pos){
nd_t *ptail=my_list->phead;
while(ptail->next!=my_list->phead)
ptail=ptail->next;
ptail->next=pnew;
pnew->next=my_list->phead;
my_list->phead=pnew;
return 0;
}
//插入节点
pnew->next=ptemp->next;
ptemp->next=pnew;
return 0;
}
int delete_list_by_head(list_t *my_list){
//传参为空或者表为空
if(NULL==my_list||NULL==my_list->phead)
return -1;
//只有一个节点
if(my_list->phead->next==my_list->phead){
free(my_list->phead);
my_list->phead=NULL;
printf("表已清空\n");
return 0;
}
//多个节点
//找到尾节点
nd_t *ptemp=my_list->phead->next;
while(ptemp->next!=my_list->phead)
{
ptemp=ptemp->next;
}
//头删
nd_t *pdel=my_list->phead;
my_list->phead=my_list->phead->next;
ptemp->next=my_list->phead;
free(pdel);
pdel=NULL;
return 0;
}
int delete_list_by_tail(list_t *my_list){
//传参为空或者表为空
if(NULL==my_list||NULL==my_list->phead)
return -1;
//只有一个节点
if(my_list->phead->next==my_list->phead){
free(my_list->phead);
my_list->phead=NULL;
printf("表已清空\n");
return 0;
}
//多个节点
//找到尾节点的前一个节点
nd_t *ptemp=my_list->phead->next;
while(ptemp->next->next!=my_list->phead)
{
ptemp=ptemp->next;
}
//尾删
nd_t *pdel=ptemp->next;
ptemp->next=my_list->phead;
free(pdel);
pdel=NULL;
return 0;
}
int delete_list_by_pos(list_t *my_list,int pos){
//传参为空或者表为空
if(NULL==my_list||NULL==my_list->phead)
return -1;
if(0>pos){
return -1;
}
//如果链表中只有一个节点
if(my_list->phead->next==my_list->phead){
//删除第一个位置的节点
if(0==pos){
free(my_list->phead);
my_list->phead=NULL;
return 0;
}
//删除其他位置节点,均是位置不合理
printf("位置不合理\n");
return -1;
}
//链表有多个节点
nd_t *pdel=my_list->phead;
//删除第一个位置的节点
//找到尾节点
nd_t *ptemp=my_list->phead;
while(ptemp->next!=my_list->phead)
{
ptemp=ptemp->next;
}
if(0==pos){
my_list->phead=my_list->phead->next;
ptemp->next=my_list->phead;
free(pdel);
pdel=NULL;
return 0;
}
//删除其他位置节点
//找到要删除的节点的前一个节点
ptemp=my_list->phead;
for(int i=0;i<pos-1;i++){
//要删除的节点的前一个节点不能是尾节点
if(ptemp->next->next==my_list->phead){
printf("删除位置不合理\n");
return -1;
}
ptemp=ptemp->next;
}
pdel=ptemp->next;
ptemp->next=pdel->next;
free(pdel);
pdel=NULL;
return 0;
}
int modify_list_by_pos(list_t *my_list,int pos,int num){
//至少有一个节点
if(NULL==my_list||NULL==my_list->phead)
return -1;
if(0>pos){
return -1;
}
//找到第pos个位置
nd_t *ptemp=my_list->phead;
for(int i=0;i<pos;i++){
if(ptemp->next==my_list->phead){
printf("位置不合理\n");
return -1;
}
ptemp=ptemp->next;
}
ptemp->data=num;
return 0;
}
int search_list_by_pos(list_t *my_list,int pos,int *num){
//至少有一个节点
if(NULL==my_list||NULL==my_list->phead||NULL==num)
return -1;
if(0>pos){
return -1;
}
//找到第pos个位置
nd_t *ptemp=my_list->phead;
for(int i=0;i<pos;i++){
if(ptemp->next==my_list->phead){
printf("位置不合理\n");
return -1;
}
ptemp=ptemp->next;
}
*num=ptemp->data;
return 0;
}
int clean_list(list_t *my_list){
if(NULL==my_list){
return -1;
}
//不止一个节点
//采用头删,先找到尾节点前一个节点
while(my_list->phead!=NULL)
{
delete_list_by_head(my_list);
}
return 0;
}
int destory_list(list_t **my_list){
if(NULL==my_list||NULL==*my_list){
return -1;
}
clean_list(*my_list);
//此时只有一个节点了
free(*my_list);
*my_list=NULL;
return 0;
}
int sort_list(list_t *my_list){
if(NULL==my_list||NULL==my_list->phead){
return -1;
}
nd_t *p=my_list->phead;
nd_t *q=NULL;
while(p->next!=my_list->phead){
q=p->next;
while(q!=my_list->phead){
if(p->data>q->data){
int temp=p->data;
p->data=q->data;
q->data=temp;
}
q=q->next;
}
p=p->next;
}
return 0;
}
int dedup_list(list_t *my_list){
//至少有一个元素
if(NULL==my_list||NULL==my_list->phead){
return -1;
}
nd_t *p=my_list->phead;
nd_t *q=NULL;
nd_t *m=NULL;
while(p->next!=my_list->phead){
m=p;
q=p->next;
while(q!=my_list->phead){
if(p->data==q->data){
m->next=q->next;
free(q);
q=m->next;
}else{
m=q;
q=q->next;