利用C語言實現靜態順序表ide
//---------- 頭文件#include "SeqList.h" --------------------函數
#pragma once測試
#include<stdio.h>
#include<stdlib.h>
#include<string.h>spa
#define MAX 100排序
//------------------ 定義數據類型 ----------------------------
typedef int DataType;string
//------------ 線性表的靜態分配順序存儲結構 ----------------
typedef struct SeqList
{
DataType arr[MAX];
int size;
}SeqList,*pSeqList;it
//---------------------- 基本操做 ----------------------------
void InitSeqList(pSeqList pslist); //初始化
void PrintSeqList(SeqList slist); //打印順序表
void PushBack(pSeqList pslist, DataType x); //尾插
void PopBack(pSeqList pslist); //尾刪
void PushFront(pSeqList pslist, DataType x); //頭插
void PopFront(pSeqList pslist); //頭刪
void Insert(pSeqList pslist, int pos, DataType x); //插入
void Remove(pSeqList pslist,DataType x); //刪除
void RemoveAll(pSeqList pslist, DataType x); //刪除全部相同節點
int Find(pSeqList pslist,DataType x); //查找
void ReverseList(pSeqList pslist); //逆序
void SortList(pSeqList pslist); //排序
int BinarySearch(pSeqList pslist,DataType x); //折半查找io
//------------- 基本操做的實現 ---------------------------
#include "SeqList.h"class
void InitSeqList(pSeqList pslist)//初始化
{
memset(pslist->arr, 0, sizeof(pslist->arr));
pslist->size = 0;
}test
void PrintSeqList(SeqList slist)//打印順序表
{
int i = 0;
for(i=0; i<slist.size; i++)
{
printf("%d ",slist.arr[i]);
}
printf("over\n");
}
void PushBack(pSeqList pslist, DataType x)//尾插
{
if(pslist->size >= MAX)
{
printf("順序表已滿!");
}
pslist->arr[pslist->size] = x;
pslist->size++;
}
void PopBack(pSeqList pslist)//尾刪
{
if(pslist->size == 0)
{
printf("順序表爲空!");
}
pslist->size--;
}
void PushFront(pSeqList pslist, DataType x)//頭插
{
int i=0;
if(pslist->size >=MAX)
{
printf("順序表已滿!");
}
for(i = pslist->size; i > 0; i--)
{
pslist->arr[i] = pslist->arr[i-1];
}
pslist->arr[0] = x;
pslist->size++;
}
void PopFront(pSeqList pslist)//頭刪
{
int i=0;
if(pslist->size == 0)
{
printf("順序表爲空");
}
for(i=0;i<pslist->size-1;i++)
{
pslist->arr[i] = pslist->arr[i+1];
}
pslist->size-- ;
}
void Insert(pSeqList pslist, int pos, DataType x) //插入
{
int i=0;
if(pslist->size >= MAX)
{
printf("順序表已滿!");
}
for(i=pslist->size;i>pos;i--)
{
pslist->arr[i] = pslist->arr[i-1];
}
pslist->arr[pos] = x;
pslist->size++ ;
}
void Remove(pSeqList pslist,DataType x) //刪除
{
int pos = Find(pslist,x);
int i = 0;
for(i = pos;i < pslist->size-1;i++)
{
pslist->arr[i] = pslist->arr[i+1];
}
pslist->size--;
}
void RemoveAll(pSeqList pslist, DataType x) //刪除全部相同節點
{
int i = 0;
int pos = Find(pslist, x);
while(pos != -1)
{
for(i = pos;i<pslist->size-1;i++)
{
pslist->arr[i] = pslist->arr[i+1];
}
pslist->size--;
pos = Find(pslist,x);
}
}
int Find(pSeqList pslist,DataType x)//查找
{
int i = 0;
for(i = 0;i < pslist->size;i++)
{
if(pslist->arr[i] == x)
{
return i;
}
}
return -1;
}
void ReverseList(pSeqList pslist)//逆序
{
int start = 0;
int end = pslist->size-1;
while(start < end)
{
DataType tmp =pslist->arr[start];
pslist->arr[start] = pslist->arr[end];
pslist->arr[end] = tmp;
start++;
end--;
}
}
void SortList(pSeqList pslist)//排序
{
int i = 0,j = 0;
for(i = 0;i<pslist->size-1;i++)
{
for(j = 0;j<=pslist->size-2-i;j++)
{
if(pslist->arr[j] > pslist->arr[j+1])
{
DataType tmp = pslist->arr[j];
pslist->arr[j] = pslist->arr[j+1];
pslist->arr[j+1] = tmp;
}
}
}
}
int BinarySearch(pSeqList pslist,DataType x)//折半查找
{
int start = 0;
int end = pslist->size-1;
while(start < end)
{
int mid = (start + end)/2;
if(x == pslist->arr[mid])
return mid;
else if(x < pslist->arr[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
return mid;
}
return 0;
}
//----------------- 測試函數 ---------------------------------
#include "SeqList.h"
void test1()
{
SeqList seqlist1;
InitSeqList(&seqlist1);
PushBack(&seqlist1,1);
PushBack(&seqlist1,2);
PushBack(&seqlist1,3);
PushBack(&seqlist1,4);
PrintSeqList(seqlist1);
PopBack(&seqlist1);
PrintSeqList(seqlist1);
PushFront(&seqlist1,8);
PrintSeqList(seqlist1);
PopFront(&seqlist1);
PrintSeqList(seqlist1);
}
void test2()
{
int ret = 0;
SeqList seqlist1;
InitSeqList(&seqlist1);
PushBack(&seqlist1,1);
PushBack(&seqlist1,2);
PushBack(&seqlist1,3);
PushBack(&seqlist1,4);
PrintSeqList(seqlist1);
Insert(&seqlist1 ,4 ,6 );
PrintSeqList(seqlist1);
ret = Find(&seqlist1, 4);
printf("%d \n" ,ret);
}
void test3(){ int ret = 0; SeqList seqlist1; InitSeqList(&seqlist1); PushBack(&seqlist1,2); PushBack(&seqlist1,1); PushBack(&seqlist1,2); PushBack(&seqlist1,3); PushBack(&seqlist1,2); PushBack(&seqlist1,4); PushBack(&seqlist1,2); PrintSeqList(seqlist1); Remove(&seqlist1,3); PrintSeqList(seqlist1); RemoveAll(&seqlist1, 2); PrintSeqList(seqlist1);}void test4(){ int ret = 0; SeqList seqlist1; InitSeqList(&seqlist1); PushBack(&seqlist1,1); PushBack(&seqlist1,2); PushBack(&seqlist1,3); PushBack(&seqlist1,4); PrintSeqList(seqlist1); ReverseList(&seqlist1); PrintSeqList(seqlist1);}void test5(){ int ret = 0; SeqList seqlist1; InitSeqList(&seqlist1); PushBack(&seqlist1,3); PushBack(&seqlist1,1); PushBack(&seqlist1,4); PushBack(&seqlist1,2); PrintSeqList(seqlist1); SortList(&seqlist1); PrintSeqList(seqlist1); ret = BinarySearch(&seqlist1, 2); printf("%d\n",ret);}int main(){ test5(); system("pause"); return 0;}