#include"head.h"
sqlist initlist()
{
sqlist S = (sqlist)malloc(sizeof(sq));
if(NULL == S)
return NULL;
memset(S->data,0,sizeof(S->data));
S->len=0;
return S;
}
int putin(sqlist S,datatype e)
{
if(NULL == S||S->len==MAXSIZE)
{
printf("i can't put in");
return -1;
}
S->data[S->len] = e;
S->len++;
return 0;
}
void output(sqlist S)
{
if(NULL == S||S->len == 0)
{
printf("i cant put out");
return;
}
for(int i=0; i<S->len; i++)
{
printf("%d ",S->data[i]);
}
puts("");
}
int insert_sub(sqlist S,int sub,datatype e)
{
if(NULL == S||S->len==MAXSIZE||sub<0||sub>S->len)
{
printf("i can't put in\n");
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_val(sqlist S,datatype e)
{
if(NULL == S||S->len==MAXSIZE)
{
printf("i can't put in\n");
return -1;
}
int sub;
for(int i=0; i<S->len; i++)
{
if(S->data[i]==e)
sub=i;
}
insert_sub(S,sub,e);
}
int del_sub(sqlist S,int sub)
{
if(NULL==S || S->len==0 ||sub<0||sub>=S->len)
{
printf("i cant delete");
return -1;
}
for(int i=sub;i<S->len;i++)
{
S->data[i]=S->data[i+1];
}
S->len--;
return 0;
}
int del_val(sqlist S,datatype e)
{
if(NULL==S || S->len==0)
{
printf("i cant delete");
return -1;
}
int sub;
for(int i=0; i<S->len; i++)
{
if(S->data[i]==e)
sub=i;
}
del_sub(S,sub);
}
void same_del(sqlist S)
{
if(NULL==S || S->len==0)
{
printf("i cant delete");
return ;
}
for(int i=0;i<S->len;i++)
{
for(int j=i+1;j<S->len;j++)
{
if(S->data[i]==S->data[j]);
{
del_sub(S,j);
j--;
}
}
}
}
#include"head.h"
Linklist init_list(_Bool flag)
{
Linklist L = (Linklist)malloc(sizeof(Node));
if(NULL == L)
return NULL;
if(flag==1)
L->len=0;
else
L->data=0;
L->next=NULL;
return L;
}
void output(Linklist L)
{
if(NULL==L||L->next==NULL)
return;
Node* p=L;
while(p->next)
{
p=p->next;
printf("%d ",p->data);
}
puts("");
}
int insert_head(Linklist L,datatype e)
{
if(NULL == L)
return -1;
Linklist s = init_list(0);
if(NULL==s)
return -1;
s->data=e;
s->next = L->next;
L->next = s;
L->len++;
return 0;
}
Node *insert_rear(Linklist L,Node* r,datatype e)
{
if(NULL == L)
return NULL;
Node* s = init_list(0);
if(NULL==s)
return NULL;
s->data = e;
r->next=s;
r=s;
L->len++;
return r;
}
int del_head(Linklist L)
{
if(NULL==L||L->next==NULL)
return -1;
Node* p = L->next;
L->next=p->next;
free(p);
p=NULL;
L->len--;
return 0;
}
int del_rear(Linklist L)
{
if(NULL==L||L->next==NULL)
return -1;
Node* p = L;
while(p->next->next)
p=p->next;
free(p->next);
p->next = NULL;
L->len--;
return 0;
}
int insert_pos(Linklist L,int pos,int e)
{
if(NULL==L||pos<0||pos>L->len+1)
return -1;
Node* s=init_list(0);
Node* p=L;
for(int i=0;i<pos;i++)
p=p->next;
s->data = e;
s->next = p->next;
p->next = s;
L->len++;
return 0;
}
int del_pos(Linklist L,int pos)
{
if(NULL==L||L->next==NULL||pos<1||pos>L->len)
return -1;
Node* p=L;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
Node* q = p->next;
p->next = q->next;
free(q);
q=NULL;
return 0;
}
int search_value(Linklist L,datatype e)
{
if(NULL == L||L->next==NULL)
return -1;
Node* p=L;
int i=0;
while(p->next)
{
p=p->next;
i++;
if(p->data==e)
return i;
}
return -1;
}
int rev_list(Linklist L)
{
if(NULL==L||L->next==NULL)
return -1;
Node* p = L->next;
L->next=NULL;
while(p)
{
Node* q = p;
p=p->next;
q->next = L->next;
L->next = q;
}
}
void bubble_sort(Linklist L)//冒泡排序
{
//判存,判空
if(NULL == L||NULL == L->next)
return ;
//双层循环
Node* p=L->next;
Node* q=L->next;
while(p->next)
{
int count=0;
while(q->next)//本来是n-i,这里全部遍历了
{
if(q->data < q->next->data)
{
datatype t = q->data;
q->data = q->next->data;
q->next->data = t;
count++;
}
q=q->next;
}
if(count==0)
break;
p=p->next;
}
}
void simple_sort(Linklist L)//简单选择排序
{
//判存,判空
if(NULL == L||NULL == L->next)
return ;
//双层循环
Node* p=L->next;
datatype t;
while(p->next)
{
Node* min = p;
Node* q=p->next;
while(q)
{
if(min->data > q->data)
{
min = q;
}
q=q->next;
}
if(min!=p)
{
t=min->data;
min->data=p->data;
p->data=t;
}
p=p->next;
}
}
#include"head.h"
DoubleList init_list(_Bool flag)
{
DoubleList L = (DoubleList)malloc(sizeof(Node));
if(NULL==L)
return NULL;
if(flag==1)
L->len=0;
else
strcpy(L->data,"");
L->next=NULL;
L->prev=NULL;
return L;
}
int output(DoubleList L)//输出
{
if(NULL==L||NULL==L->next)//判存判空
return -1;
Node* p = L->next;
while(p)//正向
{
printf("%s",p->data);
p=p->next;
}
puts("");
/*//逆向
Node*q = L;
while(q->next)
{
q=q->next;
}
while(q->prev)
{
printf("%s",q->data);
q=q->prev;
}*/
return 0;
}
int insert_head(DoubleList L,datatype e)//头插
{
if(NULL == L)//判存
return -1;
//创建后初始化插入结点
DoubleList s=init_list(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;
}
Node* insert_rear(DoubleList L,Node*r,datatype e)//尾插
{
if(NULL == L)
return NULL;
//创建后初始化结点
DoubleList s = init_list(0);
strcpy(s->data,e);
//尾插
r->next=s;
s->prev=r;
r=s;
return r;
}
int delete_head(DoubleList L)//头删
{
//判存,判空
if(NULL==L||NULL==L->next)
return -1;
//删除结点
Node* p = L->next;
L->next=p->next;
if(p->next)
p->next->prev=L;
free(p);
p=NULL;
L->len--;
return 0;
}
Node* delete_rear(DoubleList L,Node* r)//尾删
{
//判存,判空
if(NULL==L||NULL==L->next)
return NULL;
//删除结点
Node* p = r;
r->prev=NULL;
r=r->prev;
free(p);
p=NULL;
return r;
}
#include"head.h"
q init_list()//初始化
{
q q_list = (q)malloc(sizeof(queue));
if(NULL == q_list)
return NULL;
memset(q_list->data,0,sizeof(q_list->data));
q_list->rear=0;
q_list->front=0;
return q_list;
}
void output(q q_list)//输出
{
//判存,判空
if(NULL==q_list||q_list->rear==q_list->front)
{
puts("队空");
return;
}
//遍历打印
int i = q_list->front;
while(i != q_list->rear)
{
printf("%d ",q_list->data[q_list->front]);
i= (i+1)%MAXSIZE;
}
return;
}
int enqueue(q q_list,datatype e)//入队
{
//判存、判满
if(NULL==q_list||q_list->front==(q_list->rear+1)%MAXSIZE)
return -1;
q_list->data[q_list->rear]=e;
q_list->rear=(q_list->rear+1)%MAXSIZE;
return 0;
}
int dequeue(q q_list)//出队
{
//判存,判空
if(NULL==q_list||q_list->rear==q_list->front)
return -1;
//直接front前移
datatype num = q_list->data[q_list->front];
q_list->front=(q_list->front+1)%MAXSIZE;
return num;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char datatype;
typedef struct Node
{
datatype data;
struct Node* lchild;
struct Node* rchild;
}btree,*tree;
tree init_tree()//创建树
{
datatype e;
printf("请输入结点的值");
scanf(" %c",&e);
if(e=='*')
return NULL;
tree T = (tree)malloc(sizeof(btree));
if(NULL == T)
{
return NULL;
}
T->data = e;
T->lchild = init_tree();
T->rchild = init_tree();
return T;
}
void first(tree T)//先序
{
if(T==NULL)
return ;
printf("%c ",T->data);
first(T->lchild);
first(T->rchild);
}
void mid(tree T)//中序
{
if(T==NULL)
return ;
mid(T->lchild);
printf("%c ",T->data);
mid(T->rchild);
}
void last(tree T)//后序
{
if(T==NULL)
return ;
last(T->lchild);
last(T->rchild);
printf("%c ",T->data);
}
int main(int argc, const char *argv[])
{
tree T = init_tree();
first(T);
puts("");
mid(T);
puts("");
last(T);
puts("");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void insert_sort(int* arr,int len)
{
int j=0;
for(int i=1;i<len;i++)
{
int temp=arr[i];
for(int j=i-1;j>=0;j--)
{
if(arr[j]>temp)
{
arr[j+1]=arr[j];
}
else
break;
}
arr[j+1] = temp;
}
}
int main(int argc, const char *argv[])
{
int arr[10]={1,5,2,4,3,7,6,9,8,0};
int len=sizeof(arr)/sizeof(arr[0]);
insert_sort(arr,len);
for(int i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
return 0;
}