单链表剩余功能实现







源码(功能代码)
cpp
#include "head.h"
int empty_sq(list_p sqlist)
{
if(sqlist->len==0)
{
return 1;
}
else{return 0;}
}
int full_sq(list_p sqlist)
{
//检测顺序表是否已满
if(sqlist->len==MAX)
{
return 1;
}
else{return 0;}
}
list_p create_list()
{
list_p sqlist=(list_p)malloc(sizeof(list));
if(sqlist==NULL)
{
printf("申请空间失败\n");
return NULL;
}
bzero(sqlist,sizeof(sqlist->data));
sqlist->len=0;
return sqlist;
}
void head_insert(list_p sqlist,int element)
{
//判断顺序表是否存在,是否有其他元素
if(sqlist==NULL)
{
printf("入参为空");
return ;
}
if(full_sq(sqlist))
{
printf("%d",sqlist->len);
printf("顺序表yy已满");
return;
}
//头部插入
for(int i=sqlist->len-1;i>=0;i--)
{
sqlist->data[i+1]=sqlist->data[i];
}
sqlist->data[0]=element;
sqlist->len++;
}
void head_del(list_p sqlist)
{
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return;
}
if(empty_sq(sqlist))
{
printf("顺序表为空,头删无效\n");
return;
}
for(int i=0;i<=sqlist->len-1;i++)
{
sqlist->data[i]=sqlist->data[i+1];
}
sqlist->len--;
}
void outputs(list_p sqlist)
{
//判断顺序表是否存在,是否有其他元素
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(empty_sq(sqlist))
{
printf("顺序表为空\n");
return;
}
for(int i=0;i<sqlist->len;i++)
{
printf("%-3d",sqlist->data[i]);
}
putchar(10);
}
//按位插入
void pos_insert(list_p sqlist,int pos,int element)
{
//判断顺序表是否存在,是否有其他元素
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(full_sq(sqlist))
{
printf("顺序表满\n");
return;
}
if(pos>=sqlist->len+1||pos<=0)
{
printf("位置不合理\n");
return;
}
//用循环找到插入的位
for(int i=sqlist->len-1;i>=pos-1;i--)
{
//后移pos之后的数
sqlist->data[i+1]=sqlist->data[i];
}
//长度加
sqlist->len++;
//pos置位
sqlist->data[pos-1]=element;
}
void pos_del(list_p sqlist,int pos)
{
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(empty_sq(sqlist))
{
printf("顺序表为空\n");
return;
}
if(pos>=sqlist->len||pos<=0)
{
printf("位置不合理\n");
return;
}
//循环找到pos位
for(int i=pos-1;i<sqlist->len;i++)
{
//循环前移
sqlist->data[i]=sqlist->data[i+1];
}
sqlist->len--;
}
void quchong(list_p sqlist)
{
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(empty_sq(sqlist))
{
printf("顺序表为空\n");
return;
}
//双层循环找到同类
for(int i=0;i<sqlist->len;i++)
{
for(int j=sqlist->len-1;j>i;j--)
{
if(sqlist->data[i]==sqlist->data[j])
{
sqlist->data[j]=sqlist->data[j+1];
sqlist->len--;
}
}
}
}
void last_del(list_p sqlist)
{
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(empty_sq(sqlist))
{
printf("顺序表为空\n");
return;
}
sqlist->len--;
}
void last_insert(list_p sqlist,int element)
{
if(sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
if(full_sq(sqlist))
{
printf("顺序表满\n");
return;
}
sqlist->data[sqlist->len]=element;
sqlist->len++;
}
void break_sq(list_p * sqlist)
{
if(sqlist==NULL||*sqlist==NULL)
{
printf("顺序表不存在\n");
return ;
}
free(*sqlist);
*sqlist=NULL;
}
单向循环链表














源码(功能性代码)
cpp
#include "rlink.h"
//判存
int exist(node_p head)
{
if(head==NULL)
{
printf("判存:链表不存在\n");
return 1;
}
return 0;
}
//判空
int empty(node_p head)
{
if(head->next==head)
{
printf("判空:链表为空\n");
return 1;
}
return 0;
}
//申请头结点
node_p create_head()
{
//申请空间
node_p head=(node_p)malloc(sizeof(node));
//判断申请失败
if(head==NULL)
{
printf("头结点申请失败\n");
return NULL;
}
//初始化头结点
head->len=0;
head->next=head;
return head;
}
//申请节点
node_p create_node(int element)
{
node_p p=(node_p)malloc(sizeof(node));
if(p==NULL)
{
printf("节点申请失败\n");
return NULL;
}
p->data=element;
p->next=p;
return p;
}
//头插
void head_insert(node_p head,int element)
{
//判存
if(exist(head))
{
printf("头插:判存出现问题\n");
return ;
}
//头插
//创建新节点给新插入的元素用
node_p p=create_node(element);
p->next=head->next;
head->next=p;
head->len++;
}
//尾插
void last_insert(node_p head,int element)
{
//判存
if(exist(head))
{
printf("尾插:判存出现问题\n");
return;
}
//循环找尾
node_p p=create_node(element);
node_p q=head;
//判断:如果循环链表是空的,直接插入
if(head->next==head)
{
head->next=p;
p->next=head;
head->len++;
return;
}
//不是空的,正常找尾,插入
while(q->next!=head)
{
q=q->next;
}
q->next=p;
p->next=head;
head->len++;
}
//输出
void output(node_p head)
{
//判存
if(exist(head))
{
printf("输出:判存出现问题\n");
return;
}
//判空
if(empty(head))
{
printf("输出:判空出现问题\n");
return;
}
//循环输出
node_p p=head->next;
for(int i=0;i<head->len;i++)
{
printf("%-4d",p->data);
p=p->next;
}
printf("\n");
}
//头删
void head_del(node_p head)
{
//判存
if(exist(head))
{
printf("头删:判存出现问题\n");
return;
}
//判空
if(empty(head))
{
printf("头删:判空出现问题\n");
return;
}
//覆盖head->next
node_p del=head->next;
head->next=head->next->next;
free(del);
del=NULL;
head->len--;
}
//尾删
void last_del(node_p head)
{
//判存
if(exist(head))
{
printf("尾删:判存出现问题\n");
return;
}
//判空
if(empty(head))
{
printf("头删:判存出现问题\n");
return;
}
//循环找尾
node_p p=head->next;
while(p->next->next!=head)
{
p=p->next;
}
node_p del=p->next;
p->next=head;
free(del);
del=NULL;
head->len--;
}
//增删改查
//判断位置是否合理
int pos_right(node_p head,int pos)
{
if(pos>head->len||pos<=0)
{
printf("位置不合理\n");
return 1;
}
return 0;
}
//按位增
void pos_insert(node_p head,int pos,int element)
{
//判存
if(exist(head))
{
printf("按位增:判存出现问题\n");
return ;
}
//判位
if(pos_right(head,pos))
{
printf("按位增:判位出现问题\n");
return ;
}
//找到pos位
node_p p=head->next;
//个人认为0是head位置,1是head->next
int i=1;
//循环找pos-1位置
while(i++!=pos-1)
{
p=p->next;
}
//概念:找到pos位置的前一位
//那么新元素就可以根据前一位的->next插入pos的位置
node_p q=create_node(element);
q->next=p->next;
p->next=q;
head->len++;
}
void pos_del(node_p head,int pos)
{
//判存
if(exist(head))
{
printf("按位删:判存出现问题\n");
return ;
}
//判空
if(empty(head))
{
printf("按位删:判空出现问题\n");
return ;
}
//判位
if(pos_right(head,pos))
{
printf("按位删:判位出现问题\n");
return ;
}
//找到pos位
node_p p=head->next;
//个人认为0是head位置,1是head->next
int i=1;
//循环找pos-1位置
while(i++!=pos-1)
{
p=p->next;
}
//存储pos->next
node_p del=p->next;
//链接pos-1和pos->next
p->next=del->next;
//释放del
free(del);
del=NULL;
head->len--;
}
//按位改
void pos_mod(node_p head,int pos,int element)
{
//判存
if(exist(head))
{
printf("按位改:判存出现问题\n");
return ;
}
//判位
if(pos_right(head,pos))
{
printf("按位改:判位出现问题\n");
return ;
}
//找到pos位
node_p p=head->next;
//个人认为0是head位置,1是head->next
int i=1;
//循环找pos位置
while(i++!=pos)
{
p=p->next;
}
p->data=element;
}
//按位查
int pos_find(node_p head,int pos)
{
//判存
if(exist(head))
{
printf("按位查:判存出现问题\n");
return -1;
}
//判空
if(empty(head))
{
printf("按位查:判空出现问题\n");
return -1;
}
//判位
if(pos_right(head,pos))
{
printf("按位查:判位出现问题\n");
return -1;
}
//找到pos位
node_p p=head->next;
//个人认为0是head位置,1是head->next
int i=1;
//循环找pos位置
while(i++!=pos)
{
p=p->next;
}
int element=p->data;
return element;
}
//按元素查
int element_find(node_p head,int element)
{
//判存
if(exist(head))
{
printf("按元素查:判存出现问题\n");
return -1;
}
//判空
if(empty(head))
{
printf("按元素查:判空出现问题\n");
return -1;
}
node_p p=head->next;
//循环:元素匹配
int pos=1;
while(p->next!=head)
{
p=p->next;
pos++;
if(p->data==element)
{
printf("第%d个元素是:%d\n",pos,element);
return pos;
}
}
printf("按元素查:找不到该元素!\n");
return -1;
}
//按元素删
void element_del(node_p head,int element)
{
//判存
if(exist(head))
{
printf("按元素删:判存出现问题\n");
return ;
}
//判空
if(empty(head))
{
printf("按元素删:判空出现问题\n");
return ;
}
//调用:按元素查
//找pos-1位
node_p p=head->next;
int pos=1;
int pos1=element_find(head,element)-1;
while(pos!=pos1)
{
pos++;
p=p->next;
}
node_p del=p->next;
p->next=del->next;
free(del);
del=NULL;
head->len--;
}
//按元素改
void element_mod(node_p head,int element,int element1)
{
//判存
if(exist(head))
{
printf("按元素改:判存出现问题\n");
return ;
}
//判空
if(empty(head))
{
printf("按元素改:判空出现问题\n");
return ;
}
//调用:按元素查
//找pos位
node_p p=head->next;
int pos=1;
int pos1=element_find(head,element);
while(pos!=pos1)
{
pos++;
p=p->next;
}
p->data=element1;
}
//逆置
void rlink_invert(node_p head)
{
//判存
if(exist(head))
{
printf("逆置:判存出现问题\n");
return ;
}
//判空
if(empty(head))
{
printf("逆置:判空出现问题\n");
return ;
}
node_p p=head;
node_p q=head->next;
node_p r=head;
while(q!=head)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=p;
}
//清空
void rlink_clear(node_p head)
{
//判存
if(exist(head))
{
printf("清空:判存出现问题\n");
return ;
}
//判空
if(empty(head))
{
printf("清空:判空出现问题\n");
return ;
}
//循环清空
node_p p=head->next;
node_p temp;
while(p!=head)
{
temp=p;
p=p->next;
free(temp);
}
head->next=head;
head->len=0;
}
//销毁
void rlink_break(node_p *head)
{ //判存
if(exist(*head))
{
printf("销毁:判存出现问题\n");
}
node_p p=*head;
rlink_clear(p);
free(p);
*head=NULL;
}





