//mian.c
#include "MySeqList.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int s = 0, i = 0, r = 0;
SeqList list;
ElementType e;
init(&list);
while(1)
{
printf("/********************请选择操作***********************/\n");
printf("/*1头插 2尾插 3显示 4头删 5尾删 */\n");
printf("/*6按位置插入 7查找 8长度 9排序 */\n");
printf("/*10按位置删除 11按值删除 12逆序 13清除 */\n");
printf("/*14摧毁 15初始化 16退出 */\n");
printf("/****************************************************/\n");
scanf_s("%d", &s);
switch(s)
{
case 1:
printf("清输入数据,-1退出:\n");
while (scanf_s(" %d", &e), e != -1)
{
push_fornt(&list, e);
}
break;
case 2:
printf("清输入数据,-1退出:\n");
while (scanf_s(" %d", &e), e != -1)
{
push_back(&list, e);
}
break;
case 3:
show(list);
break;
case 4:
pop_fornt(&list, &e);
printf("删除的数据是%d\n", e);
break;
case 5:
pop_back(&list, &e);
printf("删除的数据是%d\n", e);
break;
case 6:
printf("请输入插入位置\n");
scanf_s("%d", &i);
printf("清输入带插入的数据, -1退出\n");
while (scanf_s(" %d", &e), e != -1)
{
insert_pos(&list, i, e);
}
break;
case 7:
printf("清输入要查找的值\n");
scanf_s("%d", &e);
r = find(list, e);
if( -1 != r )
printf("值的位置在%d\n", r);
break;
case 8:
printf("顺序的长度为%d\n", length(list));
break;
case 9:
sort(&list);
break;
case 10:
printf("请输入带删除位置\n");
scanf_s("%d", &i);
if( delete_pos(&list, i, &e) )
printf("被删除的数据时%d\n", e);
break;
case 11:
printf("清输入待删除的值\n");
scanf_s("%d", &e);
delete_val(&list, e);
break;
case 12:
reverse(&list);
break;
case 13:
clear(&list);
break;
case 14:
destroy(&list);
break;
case 15:
init(&list);
break;
case 16:
goto Exit;
break;
default:
goto Exit;
break;
}
}
Exit:
return 1;
}
//MySeqList.h
/****************************************/
/*初始化 头插 尾插 显示 头删*/
/*尾删 按位置插入 查找 长度 */
/*按位删除 按值删除 排序 逆序*/
/*清除 摧毁 合并 */
/***************************************/
//代码可以进一步优化
#ifndef MYSEQLIST_H_
#define MYSEQLIST_H_
#define MAXSIZE 10
#define INCREASE 10
typedef int ElementType;
typedef enum
{
false_,
true_
}bool_;
typedef struct SeqList
{
ElementType *Base;
int capatity;
int size;
}SeqList;
bool_ init(SeqList *mylist);
bool_ increase(SeqList *mylist);
bool_ push_fornt(SeqList *mylist, ElementType e);
bool_ push_back(SeqList *mylist, ElementType e);
void show(SeqList mylist);
bool_ pop_fornt(SeqList *mylist, ElementType *e);
bool_ pop_back(SeqList *mylist, ElementType *e);
bool_ insert_pos(SeqList *mylist,int i, ElementType e);
int find(SeqList mylist, ElementType e);
int length(SeqList mylist);
bool_ delete_pos(SeqList *mylist, int i, ElementType *e);
bool_ delete_val(SeqList *mylist, ElementType e);
void sort(SeqList *mylist);
void reverse(SeqList *mylist);
void clear(SeqList *mylist);
void destroy(SeqList *mylist);
bool_ merge(SeqList *list, SeqList* lista, SeqList *listb);
#endif //MYSEQLIST_H_
//MySeqList.c
#include "MySeqList.h"
#include "stdlib.h"
#include "stdio.h"
bool_ init(SeqList *mylist)
{
mylist->Base = (ElementType *)malloc(MAXSIZE * sizeof(ElementType));
if( NULL == mylist->Base )
return false_;
mylist->capatity = MAXSIZE;
mylist->size = 0;
return true_;
}
bool_ increase(SeqList *mylist)
{
ElementType *newspace = (ElementType *)realloc(mylist->Base, sizeof(ElementType) * ( MAXSIZE + INCREASE));
if( NULL == newspace )
{
printf("内存空间已满,增加空间失败\n");
return false_;
}
mylist->Base = newspace;
mylist->capatity += INCREASE;
return true_;
}
bool_ push_fornt(SeqList *mylist, ElementType e)
{
int i = 0;
if( mylist->size >= mylist->capatity && !increase(mylist) )
{
printf("顺序表已满\n");
return false_;
}
for(i = mylist->size; i > 0; i--)
{
mylist->Base[i] = mylist->Base[i - 1];
}
mylist->Base[0] = e;
mylist->size++;
return true_;
}
bool_ push_back(SeqList *mylist, ElementType e)
{
int i = 0;
if( mylist->size >= mylist->capatity && !increase(mylist) )
{
printf("顺序表已满\n");
return false_;
}
mylist->Base[mylist->size] = e;
mylist->size++;
return true_;
}
void show(SeqList mylist)
{
int i = 0;
if( 0 == mylist.size || NULL == mylist.Base )
{
printf("顺序表中不存在任何元素\n");
return ;
}
printf("顺序表中的元素有:");
for(i = 0; i < mylist.size; i++)
printf("%d ", mylist.Base[i]);
printf("\n");
}
bool_ pop_fornt(SeqList *mylist, ElementType *e)
{
int i = 0;
if( 0 == mylist->size )
{
printf("顺序表中没有元素\n");
return false_;
}
*e = mylist->Base[0];
for(i = 0; i < mylist->size - 1; i++)
{
mylist->Base[i] = mylist->Base[i + 1];
}
mylist->size--;
return true_;
}
bool_ pop_back(SeqList *mylist, ElementType *e)
{
if( 0 == mylist->size )
{
printf("线性表中没有任何元素\n");
return false_;
}
*e = mylist->Base[mylist->size-1];
mylist->size--;
return true_;
}
bool_ insert_pos(SeqList *mylist, int i, ElementType e)
{
int j = 0;
if( mylist->size >= mylist->capatity && !increase(mylist) )
{
printf("顺序表已满,不能插入数据\n");
return false_;
}
if( i > mylist->size || i < 0 )
{
printf("插入位置不合法\n");
return false_;
}
for(j = mylist->size; i < j; j--)
{
mylist->Base[j] = mylist->Base[j - 1];
}
mylist->Base[j] = e;
mylist->size++;
return true_;
}
int find(SeqList mylist, ElementType e)
{
int i = 0;
for(i = mylist.size; i > 0; i--)
{
if( mylist.Base[i - 1] == e )
return i - 1;
}
printf("元素%d不存在\n", e);
return -1;
}
int length(SeqList mylist)
{
return mylist.size;
}
bool_ delete_pos(SeqList *mylist, int i, ElementType *e)
{
int j = 0;
if( 0 == mylist->size )
{
printf("顺序表中不存在任何元素\n");
return false_;
}
if( i < 0 || i > mylist->size )
{
printf("删除位置不合法:");
return false_;
}
*e = mylist->Base[i];
for(j = i; j < mylist->size - 1; j++)
{
mylist->Base[j] = mylist->Base[j + 1];
}
mylist->size--;
return true_;
}
bool_ delete_val(SeqList *mylist, ElementType e)
{
int i = 0, j = 0;
if( -1 == ( i = find(*mylist, e)) )
{
printf("元素%d不存在\n", e);
return false_;
}
for(j = i; j < mylist->size - 1 ; j++)
{
mylist->Base[j] = mylist->Base[j + 1];
}
mylist->size--;
return true_;
}
void sort(SeqList*mylist)
{
int i = 0, j = 0;
ElementType tem;
if( mylist->size < 2 )
return ;
//冒泡排序
for(i = 0; i < mylist->size - 1; i++)
{
for(j = 0; j < mylist->size - 1 - i; j++)
{
if( mylist->Base[j] > mylist->Base[j + 1] )
{
tem = mylist->Base[j];
mylist->Base[j] = mylist->Base[j + 1];
mylist->Base[j + 1] = tem;
}
}
}
}
void reverse(SeqList *mylist)
{
ElementType *low, *high;
ElementType tem;
if( mylist->size < 2 )
return ;
for(low = mylist->Base, high = &(mylist->Base[mylist->size - 1]); \
low < high; low++, high--)
{
tem = *low;
*low = *high;
*high = tem;
}
}
void clear(SeqList *mylist)
{
mylist->size = 0;
}
void destroy(SeqList *mylist)
{
free(mylist->Base);
mylist->size = 0;
mylist->capatity = 0;
}
//按序合并两个有序的顺序表
bool_ merge(SeqList *list, SeqList* lista, SeqList *listb)
{
int i = 0;
int ia = 0;
int ib = 0;
list->Base = (ElementType *)malloc((sizeof(ElementType)) * (lista->size + listb->size));
if( NULL == list->Base )
return false_;
list->capatity = lista->size + listb->size;
//谁小谁被放入
while(i < lista->size && i < listb->size)
{
if(lista->Base[ia] > listb->Base[ib])
list->Base[i++] = listb->Base[ib++];
else
list->Base[i++] = lista->Base[ia++];
}
//将剩下的直接放入
while(i < lista->size)
{
list->Base[i++] = lista->Base[ia++];
}
while(i < listb->size)
{
list->Base[i++] = listb->Base[ib++];
}
list->size = lista->size + listb->size;
return true_;
}