1.顺序表插入+删除,顺序表去重
#include "head.h"
seqlist *create()
{
seqlist *p=malloc(sizeof(seqlist));
if(p==NULL)
return NULL;
p->len = 0;
return p;
}
void sfree(seqlist **p)
{
if(*p==NULL)
return ;
free(*p);
*p = NULL;
}
static int ifempty(seqlist *p)
{
return 0==p->len?1:0;
}
static int iffull(seqlist *p)
{
return N==p->len?1:0;
}
int output(seqlist *p)
{
if(NULL==p)
{
puts("不存在");
return -1;
}
else if(ifempty(p))
{
puts("为空");
return -1;
}
for(int i=0;i<p->len;++i)
printf("%d ",p->data[i]);
printf("\n");
return 0;
}
int insertsub(seqlist *p,int sub,datatype d)
{
if(NULL==p)
{
puts("不存在");
return -1;
}
else if(iffull(p))
{
puts("为满");
return -1;
}
else if(sub>p->len||sub<0)
{
puts("下标错误");
return -1;
}
for(int i=p->len-1;i>=sub;--i)
p->data[i+1]=p->data[i];
p->data[sub]=d;
++p->len;
return 0;
}
int deletesub(seqlist *p,int sub)
{
if(NULL==p)
{
puts("不存在");
return -1;
}
else if(ifempty(p))
{
puts("为空");
return -1;
}
else if(sub>=p->len||sub<0)
{
puts("下标错误");
return -1;
}
for(int i=sub;i<p->len;++i)
p->data[i]=p->data[i+1];
--p->len;
return 0;
}
int deduplicate(seqlist *p)
{
if(NULL==p)
{
puts("不存在");
return -1;
}
else if(ifempty(p))
{
puts("为空");
return -1;
}
for(int i=0;i<p->len-1;++i)
for(int j=i+1;j<p->len;++j)
if(p->data[i]==p->data[j])
{
deletesub(p,j);
--j;
--i;
}
return 0;
}
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 20 //定义长度
typedef int datatype;//元素类型
typedef struct{
datatype data[N];//数据元素
int len;//长度
}seqlist;
seqlist *create();//创建
void sfree(seqlist **p);//释放
static int ifempty(seqlist *p);//判空
static int iffull(seqlist *p);//判满
int output(seqlist *p);//输出
int insertsub(seqlist *p,int sub,datatype d);//下标插入
int deletesub(seqlist *p,int sub);//下标删除
int deduplicate(seqlist *p);//去重
#endif
#include "head.h"
int main()
{
seqlist *s = create();
printf("输入个数:");
int n;
datatype a;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("输入第%d个元素:",i+1);
scanf("%d",&a);
insertsub(s,i,a);
}
output(s);
printf("输入删除的元素下标:");
scanf("%d",&n);
deletesub(s,n);
output(s);
puts("去重");
deduplicate(s);
output(s);
sfree(&s);
return 0;
}
2. 链表头插尾插头删删尾删
#ifndef __HEAD_H__
#define __HEAD_H____
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int datatype;
typedef struct Node{
union{
int len;
datatype data;
};
struct Node *next;
}*Linklist,node;
Linklist create();//创建
void lfree(Linklist *list);//释放
int insertHead(Linklist list,datatype e);//头插
int output(Linklist list);//输出
int insertRear(Linklist list,datatype e);//尾插
int deleteHead(Linklist list);//头删
int deleteRear(Linklist list);//尾删
int inverse(Linklist list);//逆置
int selectSort(Linklist list);//选择排序
int bubbleSort(Linklist list);//冒泡
#include "head.h"
Linklist create()
{
Linklist list = malloc(sizeof(node));
if(NULL == list)
return NULL;
list->len = 0;
list->next = NULL;
return list;
}
void lfree(Linklist *list)
{
Linklist t = NULL;
while((*list)!=NULL)
{
t = (*list)->next;
free(*list);
*list = t;
}
}
int insertHead(Linklist list,datatype e)
{
Linklist n = create();
if(NULL==n||NULL==list)
return -1;
n->data = e;
n->next = list->next;
list->next = n;
list->len++;
return 0;
}
int output(Linklist list)
{
if(NULL==list||!list->len)
return -1;
while(NULL!=list->next)
{
list = list->next;
printf("%d ",list->data);
}
printf("\n");
return 0;
}
int insertRear(Linklist list,datatype e)
{
if(NULL==list)
return -1;
Linklist t = list;
while(NULL!=list->next)
{
list = list->next;
}
Linklist n = create();
if(n==NULL)
return -1;
list->next = n;
n->data = e;
t->len++;
return 0;
}
int deleteHead(Linklist list)
{
if(NULL==list||!list->len)
return -1;
Linklist t = list->next;
list->next = t->next;
free(t);
list->len--;
return 0;
}
int deleteRear(Linklist list)
{
if(NULL==list||!list->len)
return -1;
Linklist t = list;
while(NULL!=list->next->next)
{
list = list->next;
}
free(list->next->next);
list->next = NULL;
t->len--;
}
int inverse(Linklist list)
{
if(list->next==NULL)
return -1;
Linklist t = list->next,p = NULL;
list->next = NULL;
while(t!=NULL)
{
p = t->next;
t->next = list->next;
list->next = t;
t = p;
}
return 0;
}
int selectSort(Linklist list)
{
if(list == NULL||list->next == NULL)
return -1;
Linklist i=list->next,j=NULL;
while(i->next!=NULL)
{
j = i->next;
Linklist t = i;
while(j!=NULL)
{
if(t->data<j->data)
t = j;
j = j->next;
}
if(t != i)
{
datatype td = t->data;
t->data = i->data;
i->data = td;
}
i = i->next;
}
return 0;
}
int bubbleSort(Linklist list)
{
if(list == NULL||list->next ==NULL)
return -1;
Linklist i=list->next;
while(i->next!=NULL)
{
int s=0;
Linklist j = list->next;
while(j->next!=NULL)
{
if(j->data<j->next->data)
{
datatype t = j->data;
j->data = j->next->data;
j->next->data = t;
s++;
}
j=j->next;
}
if(s==0)
break;
i=i->next;
}
}
#include "head.h"
int main(int argc,const char *argv[])
{
Linklist a = create();
int n;
printf("输入元素个数:");
scanf("%d",&n);
datatype e;
for(int i=0;i<n;++i)
{
printf("输入第%d个元素:",i+1);
scanf("%d",&e);
insertHead(a,e);
}
output(a);
printf("输入尾插的元素:");
scanf("%d",&e);
insertRear(a,e);
output(a);
puts("头删");
deleteHead(a);
output(a);
puts("尾删");
deleteRear(a);
output(a);
lfree(&a);
return 0;
}
3.链表逆置,链表排序
#include "head.h"
int main(int argc,const char *argv[])
{
Linklist a = create();
int n;
printf("输入元素个数:");
scanf("%d",&n);
datatype e;
for(int i=0;i<n;++i)
{
printf("输入第%d个元素:",i+1);
scanf("%d",&e);
insertHead(a,e);
}
puts("逆置");
inverse(a);
output(a);
puts("冒泡排序");
bubbleSort(a);
output(a);
puts("选择排序");
selectSort(a);
output(a);
lfree(&a);
return 0;
}
4. 双向链表头插头删,尾插尾删
#ifndef __HEAD_H__
#define __HEAD_H____
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char datatype[8];
typedef struct Node{
datatype data;
struct Node *next;
struct Node *last;
}*Linklist,node;
Linklist create();//创建
void lfree(Linklist *list);//释放
int insertHead(Linklist list,datatype e);//头插
int output(Linklist list);//输出
int insertRear(Linklist list,datatype e);//尾插
int deleteHead(Linklist list);//头删
int deleteRear(Linklist list);//尾删
#endif
#include "head.h"
Linklist create()
{
Linklist list = malloc(sizeof(node));
if(list == NULL)
return NULL;
list->next = NULL;
list->last = NULL;
memset(list->data,0,sizeof(datatype));
return list;
}
void lfree(Linklist *list)
{
if(*list==NULL)
return;
Linklist t = NULL;
while(*list!=NULL)
{
t = (*list)->next;
free(*list);
*list = t;
}
}
int insertHead(Linklist list,datatype e)
{
if(list == NULL)
return -1;
Linklist p = create();
if(p == NULL)
return -1;
p->next = list->next;
p->last = list;
list->next = p;
if(p->next!=NULL)
p->next->last = p;
strcpy(p->data,e);
return 0;
}
int output(Linklist list)
{
if(list == NULL)
return -1;
printf("正\n");
while(list->next!=NULL)
{
list = list->next;
printf("%s\t",list->data);
}
printf("\n逆\n");
while(list->last!=NULL)
{
printf("%s\t",list->data);
list = list->last;
}
printf("\n");
return 0;
}
int insertRear(Linklist list,datatype e)
{
if(list == NULL)
return -1;
Linklist p = create();
if(p == NULL)
return -1;
while(list->next!=NULL)
{
list = list->next;
}
list->next = p;
p->last = list;
strcpy(p->data,e);
return 0;
}
int deleteHead(Linklist list)
{
if(list == NULL||list->next==NULL)
return -1;
Linklist t = list->next;
list->next = t->next;
if(list->next!=NULL)
list->next->last = list;
free(t);
return 0;
}
int deleteRear(Linklist list)
{
if(list == NULL||list->next==NULL)
return -1;
while(list->next!=NULL)
{
list = list->next;
}
list = list->last;
free(list->next);
list->next=NULL;
return 0;
}
#include "head.h"
int main(int argc,const char *argv[])
{
Linklist a = create();
char s[8];
int n;
printf("输入元素的个数:");
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
printf("输入第%d个元素:",i);
scanf("%s",s);
insertHead(a,s);
}
output(a);
printf("输入尾插的元素:");
scanf("%s",s);
insertRear(a,s);
output(a);
puts("头删");
deleteHead(a);
output(a);
puts("尾删");
deleteRear(a);
output(a);
lfree(&a);
return 0;
}
5. 循环队列
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
typedef int datatype;
typedef struct{
datatype data[N];
int front;
int rear;
}queue;
queue *create();//创建
void qfree(queue **q);//释放
int enqueue(queue *q,datatype e);//入队
int dequeue(queue *q);//出队
#endif
#include "head.h"
queue *create()
{
queue *q = malloc(sizeof(queue));
if(q==NULL)
return NULL;
q->front = -1;
q->rear = 0;
return q;
}
void qfree(queue **q)
{
if(*q==NULL)
return ;
free(*q);
*q = NULL;
}
int enqueue(queue *q,datatype e)
{
if(q==NULL||q->front==q->rear)
return -1;
if(q->front == -1)
q->front = q->rear;
q->data[q->rear] = e;
q->rear = (q->rear+1)%N;
}
int dequeue(queue *q)
{
if(q==NULL||q->front==-1)
return -1;
datatype t = q->data[q->front++];
q->front = q->front%N;
if(q->front==q->rear)
q->front = -1;
return t;
}
#include "head.h"
int main(int argc,const char *argv[])
{
int t,e;
queue *q = create();
do
{
printf("输入1入队 2出队 -1结束:");
scanf("%d",&t);
switch(t)
{
case 1:
printf("输入元素:");
scanf("%d",&e);
enqueue(q,e);
break;
case 2:
printf("%d\n",dequeue(q));
break;
}
}while(t!=-1);
qfree(&q);
return 0;
}
6.二叉树创建和遍历
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *left;
struct node *right;
}*tree;
tree create();//创建
tree tfree(tree t);//释放
void preorder(tree t);//先序
void inorder(tree t);//中序
void postorder(tree t);//后序
#endif
#include "haed.h"
tree create()
{
char a;
scanf(" %c",&a);
if(a=='#')
return NULL;
tree t = malloc(sizeof(struct node));
if(t==NULL)
return NULL;
t->data = a;
t->left = create();
t->right = create();
return t;
}
tree tfree(tree t)
{
if(t==NULL)
return NULL;
t->left = tfree(t->left);
t->right = tfree(t->right);
free(t);
t = NULL;
return t;
}
void preorder(tree t)
{
if(t==NULL)
return;
printf("%c",t->data);
preorder(t->left);
preorder(t->right);
}
void inorder(tree t)
{
if(t==NULL)
return;
inorder(t->left);
printf("%c",t->data);
inorder(t->right);
}
void postorder(tree t)
{
if(t==NULL)
return;
postorder(t->left);
postorder(t->right);
printf("%c",t->data);
}
#include "haed.h"
int main(int argc,const char *argv[])
{
tree t = create();
puts("先序");
preorder(t);
printf("\n中序\n");
inorder(t);
puts("\n后序");
postorder(t);
puts("");
t = tfree(t);
return 0;
}
7.插入排序
void charu(int *a,int len)
{
for(int i=1;i<len;++i)
{
int t = a[i];
int j;
for(j=i-1;j>=0;--j)
{
if(t<a[j])
a[j+1] = a[j];
else
break;
}
a[j+1] = t;
}
}
int main()
{
int a[10]={5,8,9,32,3,89,1,0,5,10};
charu(a,10);
for(int i=0;i<10;++i)
{
printf("%d\t",a[i]);
}
printf("\n");
return 0;
}