【无标题】周末作业 6-18

1.顺序表插入+删除 顺序表去重

int Inset_Sub(Sqlist_p s,int sub,datatype e)
{
if(NULLs||s->lenMax_Size||sub<0||sub>s->len)
{
return -1;
}
for(int i=s->len;i>=sub;i–)
{
s->data[i+1]=s->data[i]
}
s->data[sub]=e;
s->len++;
return 0;

}
int Insert_Element(Sqlist_p s,datatype e )
{
if(sNULL||s->lenMax_Size)
{
return -1;
}
int sub;
for(int i=0;ilen;i++)
{
if(s->data[i]==e)
{
sub=i;
}
}
for(int i=s->len;i>=sub;i–)
{
s->data[i+1]=s->data[i];
}
s->data[sub]=e;
s->len++;
return 0;

}

int Delete_Sub(Sqlist_p s,int sub)
{
if(sNULL||s->len0||sub<0||sub>=s->len)
{
return -1;
}
for(int i=sub;ilen;i++)
{
s->data[i]=s->data[i+1];
}
s->len–;
return 0;
}

int Del_Element(Sqlist_p s,datatype e)
{
if(NULLs||s->len0)
{
return -1;
}
int sub;
for(int i=0;ilen;i++)
{
if(s->data[i]==e)
{
sub=i;
}
}
Delete_Sub(s,sub);
return 0;
}

void Same_Delete(Sqlist_p s)
{
if(NULL=s||s->len==0)
{
return ;
}
for(int i=0;ilen;i++)
{
for(int j=i+1;jlen;j++)
{
if(s->data[i]==s->data[j])
{
Delete_Sub(s,j);
j–;
}
}
}
}

2.链表头插+尾插+头插尾删

#include “head.h”
Linklist_p Creat(int flag)
{
Linklist_p l=(Linklist_p)malloc(sizeof(Linklist));
if(lNULL)
{
return NULL;
}
if(flag
1)
{
l->len=0;
}
else
{
l->data=0;
}
l->next=NULL;
return l;
}

int Insert_Head(Linklist_p l,datatype e)
{
if(l==NULL)
return -1;
Linklist_p s=Creat(0);
if(s=NULL)
{
return -1;
}
s->data=e;
s->next=l->next;
l->next=s;
l->len++;
return 0;
}

Linklist_p Insert_Rear(Linklist_p l,datatype e)
{
if(lNULL)
{
return NULL;
}
Linklist_p s=Creat(0);
if(NULL
s)
{
return NULL;
}
s->data=e;
Linklist_p rear=l;
while(rear->next!=NULL)
{
rear=rear->next;
}
rear->next=s;
r=s;
l->len++;
return r;
}

int Delete_Head(Linklist_p l)
{
if(lNULL||l->nextNULL)
{
return -1;
}
Linklist_p p=l->next;
l->next=p->next;
free§;
p=NULL;
l->len–;
return 0;

}
int Delete_Rear(Linklist_p l)
{
if(NULLl||l->nextNULL)
{
return -1;
}
Linklist_p p=l;
while(p->next->next)
{
p=p->next;
}
free(p->next);
p->next=NULL;
l->len–;
return 0;
}

3.链表逆置,链表排序

Linklist_p Rev_Linklist(Linklist_p l)
{
if(NULLl||l->nextNULL)
{
return NULL;
}
Linklist_p q=l;
Linklist_p p=q->next;
l->next=NULL;
while(p!=NULL)
{
Linklist_p t=p;
p=p->next;
t->next=q->next;
q->next=t;
}
return l;
}

void Bubble(Linklist_p l)
{
if(l->nextNULL||lNULL)
{
return ;
}
for(Linklist_p i=l->next;i->next!=NULL;i=i->next)
{
int count=0;
for(Linklist_p j=l->next;j->next!=NULL;j=j->next)
{
if(j->data>j->next->data)
{
datatype t=j->data;
j->data=j->next->data;
j->next->data=t;
count++;
}
}
if(count==0)
break;
}
}

在这里插入图片描述
#include “head.h”
Doublelink_p Creat(int flag)
{
Doublelink_p l=(Doublelink_p)malloc(sizeof(Doublelink));
if(lNULL)
{
return NULL;
}
if(flag
1)
{
l->len=0;
}
else
{
l->data=0;
}
l->next=NULL;
l->prev=NULL;
return l;

}
int Insert_Head(Doublelink_p l,datatype e)
{
if(l==NULL)
{
return -1;
}
Doublelink_p s=Creat(0);
strcpy(s->data,e);
s->next=l->next;
s->prev=l;
if(l->next!=NULL)
{
l->next->prev=s;
}
l->next=s;
l->len++;
return 0;
}

Doublelink_p Insert_Rear(Doublelink_p l,datatype e)
{
if(NULL==l)
{
return NULL;
}
Doublelink_p rear=l;
while(rear->next!=NULL)
{
rear=rear->next;
}
Doublelink_p s=Creat(0);
strcpy(s->data,e);
rear->next=s;
s->prev=rear;
rear=s;
return rear;
}

int Delete_Head(Doublelink_p l)
{
if(NULLl||l->nextNULL)
{
return -1;
}
Doublelink_p p=l->next;
l->next=p->next;
if(p->next!=NULL)
{
p->next->prev=l;
}
free§;
p=NULL;
l->len–;
return 0;
}

Doublelink_p Delete_Rear(Doublelink_p l,datatype e)
{
if(NULLl||NULLl->next)
{
return NULL;
}
Doublelink_p rear=l;
while(rear->next!=NULL)
{
rear=rear->next;
}
Doublelink_p p=rear;
rear->prev=NULL;
rear=rear->prev;
free§;
p=NULL;
return rear;
}

在这里插入图片描述
#include “head.h”

Queue_p Creat()
{
Queue_p q=(Queue_p)malloc(sizeof(Queue));
if(NULLq)
return NULL;
memset(q->data,0,sizeof(q->data));
q->rear=0;
q->front=0;
return q;
}
void Output(Queue_p q)
{
if(NULL
q||q->rearq->front)
{
return;
}
int i=q->front;
whle(i!=q->rear)
{
printf(“%d\t”,q->data[i]);
i=(i+1)%Max_Size;
}
return;
}
int Enqueue(Queue_p q,datatype e)
{
if(NULL
q||q->front==(q->rear+1)%Max_Size)
{
return -1;
}
q-<data[q->rear]=e;
q->rear=(q->rear+1)%Max_Size;
return 0;
}

int Dequeue(Queue_p q)
{
if(NULLq||q->rearq->front)
{
return -1;
}
datatype num=q->data[q->front];
q->front=(q->frong+1)%Max_Size;
return num;
}

6.在这里插入图片描述
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

typedef char datatype;
typedef struct Node
{
datatype data;
struct Node *lchild;
struct Node *rchild;

}Btree,*Btree_p

Btree_p Creat()
{
datatype e;
scanf(“%d”,&e);
if(e==‘*’)
{
return NULL;
}
Btree_p t=(Btree)malloc(sizeof(Btree));
if(t==NULL)
{
return NULL;
}
t->data=e;
t->lchild=Creat();
t->rchild=Creat();
return t;

}

void Frist(Btree_p t)
{
if(tNULL)
return ;
printf(“%c”,t->data);
Frist(t->lchild);
Frist(t->rchild);
}
void mid(Btree_p t)
{
if(t
NULL)
return ;

mid(t->lchild);
printf("%c",t->data);
mid(t->rchild);

}
void Last(Btree_p t)
{
if(t==NULL)
return ;

Last(t->lchild);
Last(t->rchild);
printf("%c",t->data);

}

7.在这里插入图片描述
void Inset_Sort(int* arr,int n)
{
for(int i=1;i<n;i++)
{
int temp=arr[i];
for(int j=i-1;j>=0;j–)
{
if(temp<arr[j])
{
arr[j+1]=arr[j]
}
else
{
break;
}
}
arr[j+1]=temp;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值