码迷,mamicode.com
首页 > 其他好文 > 详细

顺序表的实现---静态

时间:2016-02-27 01:08:14      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:顺序表

"Seq_S.h"

#ifndef __SEQ_LIST_H__

#define __SEQ_LIST_H__

#define MAX 100


#include <stdio.h>

#include <assert.h>

#include <string.h>


typedef int DataType;


typedef struct SeqList

{

DataType arr[MAX];

int size;

}SeqList, *pSeqList;


void InitSeqList(pSeqList pSeq);//初始化

void PrintSeqList(SeqList Seq);//打印


void PushBack(pSeqList pSeq, DataType x);//尾插

void PopBack(pSeqList pSeq);//尾删

void PushFront(pSeqList pSeq, DataType x);//头插

void PopFront(pSeqList pSeq);//头删



void Insert(pSeqList pSeq, int pos, DataType x);//在某一位置插入某个元素

void Remove(pSeqList pSeq, DataType x);//删除x元素

void RemoveAll(pSeqList pSeq, DataType x);//删除所有x元素

int Find();


void ReverseList(pSeqList pSeq);//逆序


void SortList(pSeqList pSeq);//排序

int BinarySearch(SeqList Seq, DataType x);//二分查找




void InitSeqList(pSeqList pSeq)  //初始化

{

assert(pSeq);

memset(pSeq->arr,0,sizeof(pSeq->arr));

pSeq->size = 0;

}

void PrintSeqList(SeqList Seq)  //打印

{

int i = 0;

for(i=0;i<Seq.size;i++)

{

printf("%d ",Seq.arr[i]);

}

printf("over\n");

}

void PushBack(pSeqList pSeq, DataType x) //尾插

{

assert(pSeq);

if(pSeq->size>=MAX)

{

printf("顺序表已满\n");

return;

}

else

{

pSeq->arr[pSeq->size] = x;

pSeq->size++;

}

}

void PopBack(pSeqList pSeq) //尾删

{

assert(pSeq);

if(pSeq->size == 0)

{

printf("顺序表已空\n");

return;

}

else

{

pSeq->size--;

}

}

void PushFront(pSeqList pSeq, DataType x) //头插

{

int i = 0;

assert(pSeq);

if(pSeq->size >= MAX)

{

printf("顺序表已满\n");

return;

}

else if(pSeq->size == 0)

{

pSeq->arr[pSeq->size] = x;

pSeq->size++;

}

else

{

for(i=pSeq->size-1;i>=0;i--)

{

pSeq->arr[i+1] = pSeq->arr[i];

}

pSeq->arr[0] = x;

pSeq->size++;

}

}

void PopFront(pSeqList pSeq)//头删

{

int i = 0;

assert(pSeq);

if(pSeq->size == 0)

{

printf("顺序表已空\n");

return;

}

else if(pSeq->size == 1)

{

pSeq->size--;

}

else

{

for(i=0;i<pSeq->size;i++)

{

pSeq->arr[i] = pSeq->arr[i+1];

}

pSeq->size--;

}

}


void Insert(pSeqList pSeq, int pos, DataType x)//在指定位置插入一个元素

{

int j = 0;

assert(pSeq);

if(pos<0 ||pos>MAX )

{

printf("插入位置不合法\n");

return;

}

for(j=pSeq->size-1;j>=pos-1;j--)

{

{

pSeq->arr[j+1]=pSeq->arr[j];

}

}

pSeq->arr[pos-1] = x;

pSeq->size++;

}

void Remove(pSeqList pSeq, DataType x) //删除某一个元素

{

int i = 0;

int j = 0;

assert(pSeq);

for(i=0;i<pSeq->size;i++)

{

if(pSeq->arr[i] == x)

{

for(j=i;j<pSeq->size;j++)

{

pSeq->arr[j]=pSeq->arr[j+1];

}

pSeq->size--;

return;

}

}

printf("不存在这个元素\n");

}

void RemoveAll(pSeqList pSeq, DataType x)

{

int i = 0;

int j = 0;

assert(pSeq);

for(i=0;i<pSeq->size;i++)

{

if(pSeq->arr[i] == x)

{

for(j=i;j<pSeq->size;j++)

{

pSeq->arr[j]=pSeq->arr[j+1];

}

pSeq->size--;

}

}

}


void ReverseList(pSeqList pSeq)//逆序

{

int tmp;

int left = 0;

int right = pSeq->size-1;

assert(pSeq);

while(left < right)

{

tmp = pSeq->arr[left];

pSeq->arr[left]  = pSeq->arr[right];

pSeq->arr[right] = tmp;

left++;

right--;

}

}

void SortList(pSeqList pSeq)//冒泡排序

{

int i = 0;

int j = 0;

int tmp = 0;

assert(pSeq);

for(i=0;i<pSeq->size-1;i++)

{

for(j=0;j<pSeq->size-1-i;j++)

{

if(pSeq->arr[j]>pSeq->arr[j+1])

{

tmp = pSeq->arr[j];

pSeq->arr[j] = pSeq->arr[j+1];

pSeq->arr[j+1] = tmp;

}

}

}

}

int BinarySearch(SeqList Seq, DataType x)//二分查找

{

int left = 0;

int right = Seq.size-1;

int mid = 0;

while(left <= right)

{

mid = (left+right)/2;

//printf("%d ",mid);

if(Seq.arr[mid] < x)

left = mid+1;

if(Seq.arr[mid] > x)

right = mid-1;

if(Seq.arr[mid] == x)

return Seq.arr[mid];

}

return 0;

}



#endif    //__SEQ_LIST_H__


"test.c"

#include "Seq_S.h"


void Test1()   //尾插

{

SeqList mylist;

InitSeqList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

PrintSeqList(mylist);

}

void Test2()   //尾删

{

SeqList mylist;

InitSeqList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

PopBack(&mylist);

PopBack(&mylist);

PopBack(&mylist);

PrintSeqList(mylist);

}

void Test3()    //头插头删

{

SeqList mylist;

InitSeqList(&mylist);

PushFront(&mylist,1);

PushFront(&mylist,2);

PushFront(&mylist,3);

PopFront(&mylist);

    PopFront(&mylist);

PrintSeqList(mylist);

}

void Test4()   //在指定位置插入某一元素

{

SeqList mylist;

InitSeqList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

Insert(&mylist, 2, 5);

PrintSeqList(mylist);

}

void Test5()   //删除某一元素

{

SeqList mylist;

InitSeqList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

Remove(&mylist,3);

PrintSeqList(mylist);

}

void Test6()   //逆序

{

SeqList mylist;

InitSeqList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

ReverseList(&mylist);

PrintSeqList(mylist);

}

void Test7()

{

SeqList mylist;

int ret = 0;

InitSeqList(&mylist);

    PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

PushBack(&mylist,5);

    //SortList(&mylist);   //冒泡排序

ret = BinarySearch(mylist,7);  //二分查找

if(ret != 0)

{

printf("%d ",ret);

}

else

{

printf("不存在此元素\n");

}

}

void Test8()

{

SeqList mylist;

InitSeqList(&mylist);

    PushBack(&mylist,1);

PushBack(&mylist,4);

PushBack(&mylist,3);

PushBack(&mylist,4);

PushBack(&mylist,5);

RemoveAll(&mylist,4);

PrintSeqList(mylist);

}

int main()

{

Test8();

return 0;

}


顺序表的实现---静态

标签:顺序表

原文地址:http://10810429.blog.51cto.com/10800429/1745429

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!