main.c
#include <stdio.h>
#include <stdlib.h>
#include "Seqlist.h"
int main(char argc , const char *argv[])
{
char p[10];
int elem;
int pose;
char ch;
sqList_p L=initList();
printf("----------------------\n");
while(1)
{
printf("tips;头插(ih),尾插(it),任意位置插入(ip),头删(dh),尾删(dt),任意位置删除(dp),quit(q):");
gets(p);
if(strcmp(p,"ih")==0)
{
printf("要插入的元素:");
scanf("%d",&elem);
insertList_head(L,elem);
outPutList(L);
}
else if(strcmp(p,"it")==0)
{
printf("要插入的元素");
scanf("%d",&elem);
insertList_tail(L,elem);
outPutList(L);
}
else if (strcmp(p,"ip")==0)
{
printf("要插入的元素,位置");
scanf("%d %d",&elem,&pose);
insertList_pose(L,elem,pose);
outPutList(L);
}
else if(strcmp(p,"dh")==0)
{
deleteElem_head(L);
outPutList(L);
}
else if(strcmp(p,"dt")==0)
{
deleteElem_tail(L);
outPutList(L);
}
else if(strcmp(p,"dp")==0)
{
printf("要删除元素的位置:");
scanf("%d",&pose);
deleteElem_pose(L,pose);
outPutList(L);
}
else if (strcmp(p,"dse")==0)
{
delSameElem(L);
outPutList(L);
}
else if(strcmp(p,"q")==0)
break;
while((ch=getchar())!='\n')
printf("----------------------\n");
}
return 0;
}
SqList.c
#include "Seqlist.h"
//初始化列表
sqList_p initList()
{
sqList_p L = (sqList_p)malloc(sizeof(sqList));
if(L==NULL)
{
printf("errror");
return NULL;
}
L->len =0;
bzero(L,sizeof(L));
return L;
}
//判空
int isEmpty(sqList_p L)
{
if(L==NULL)
return -1;
return L->len==0?1:0;
}
//判满
int isFull(sqList_p L)
{
if(L==NULL)
return -1;
return L->len==MAX?1:0;
}
//顺序表头插
void insertList_head(sqList_p L,typedata data)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(isFull(L))
{
printf("表以满不能插入\n");
return;
}
for(int i=L->len-1;i>=0;i--)
{
L->data[i+1]=L->data[i];
}
L->data[0] = data;
L->len++;
}
//顺序表尾插
void insertList_tail(sqList_p L,typedata data)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return ;
}
if(isFull(L))
{
printf("表已满不能插入\n");
return;
}
L->data[L->len]=data;
L->len++;
}
//在第i个位置插入
void insertList_pose(sqList_p L,typedata data,int pose)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(isFull(L))
{
printf("表已满不能插入\n");
return;
}
if(pose<0 || pose>L->len)
{
printf("位置不合法,请检查\n");
return;
}
for(int i=L->len-1;i>=pose;i--)
{
L->data[i+1]=L->data[i];
}
L->data[pose]=data;
L->len++;
}
//头删
void deleteElem_head(sqList_p L)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(L->len==0)
return;
for(int i=0;i<L->len-1;i++)
L->data[i]=L->data[i+1];
L->len--;
}
//尾删
void deleteElem_tail(sqList_p L)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(L->len==0)
{
printf("表空");
return;
}
L->len--;
}
//任意位置删除
void deleteElem_pose(sqList_p L,int pose)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(L->len==0)
{
printf("表空");
return;
}
for(int i=pose;i<L->len-1;i++)
{
L->data[i]=L->data[i+1];
}
L->len--;
}
//输出列表
void outPutList(sqList_p L)
{
if(L==NULL)
return;
printf("seqList:");
for(int i=0;i<L->len;i++)
{
printf("%d\t",L->data[i]);
}
printf("\n");
}
//按值查找
int searchElem(sqList_p L,typedata data)
{
if(L==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
if(L->len==0)
{
printf("表空\n");
return -1;
}
for(int i=0;i<L->len;i++)
{
if(L->data[i]==data)
return i;
}
printf("无此元素\n");
return -1;
}
//按位置查找
int searchPose(sqList_p L,int pose)
{
if(L==NULL)
{
printf("入参为空,请检查");
return -1;
}
if(pose<0 || pose >L->len)
{
printf("输入不合法\n");
return -1;
}
return L->data[pose];
}
//去重
void delSameElem(sqList_p L)
{
//if null
for(int i=0;i<L->len-1;i++)
{
for(int j=i+1;j<L->len;j++)
{
if(L->data[i]==L->data[j])
{
for(int k=j;k<L->len-1;k++)
{
L->data[k]=L->data[k+1];
}
L->len--;
j--;
}
}
}
}
//清空顺序表
void clearList(sqList_p L)
{
if(L==NULL)
{
return;
}
L->len=0;
}
//释放顺序表
void freeList(sqList_p *L)
{
if(L==NULL||*L==NULL) return;
free(*L);
*L=NULL;
}
//
void bubbleSort(sqList_p L)
{
for(int i=0;i<L->len-1;i++)
{
for(int j=0;j<L->len-i-1;j++)
{
if(L->data[j]>L->data[j+1])
{
typedata tmp;
tmp=L->data[j];
L->data[j]=L->data[j+1];
L->data[j+1]=tmp;
}
}
}
}
void ifSort(sqList_p L)
{
for(int i=0;i<L->len-1;i++)
{
int index=i;
for(int j=i+1;j<L->len;j++)
{
if(L->data[j]>L->data[i])
index=j;
}
int tmp;
tmp = L->data[i];
L->data[i]=L->data[index];
L->data[index]=tmp;
}
}
Sqlist.h
#ifndef SEQLIST_H_
#define SEQLIST_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
typedef int typedata;
typedef struct
{
typedata data[MAX];
int len;
}sqList,*sqList_p;
sqList_p initList();
int isEmpty(sqList_p L);
int isFull(sqList_p L);
void insertList_head(sqList_p L,typedata data);
void outPutList(sqList_p L);
void insertList_tail(sqList_p L,typedata data);
void insertList_pose(sqList_p L,typedata data,int pose);
void deleteElem_head(sqList_p L);
void deleteElem_tail(sqList_p L);
void deleteElem_pose(sqList_p L,int pose);
int searchElem(sqList_p L,typedata data);
int searcPose(sqList_p L,int pose);
void delSameElem(sqList_p L);
void clearList(sqList_p L);
void freeList(sqList_p *L);
void bubbleSort(sqList_p L);
void ifSort(sqList_p L);
#endif