利用C语言实现顺序表

       利用C语言实现静态顺序表


//---------- 头文件#include "SeqList.h" --------------------

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 100


//------------------ 定义数据类型 ----------------------------
typedef int DataType;


//------------ 线性表的静态分配顺序存储结构 ----------------
typedef struct SeqList
{
 DataType arr[MAX];
 int size;
}SeqList,*pSeqList;


//---------------------- 基本操作 ----------------------------
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);       //折半查找


//------------- 基本操作的实现 ---------------------------
#include "SeqList.h"

void InitSeqList(pSeqList pslist)//初始化
{
 memset(pslist->arr, 0, sizeof(pslist->arr));
 pslist->size = 0;
}

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;
}


本文出自 “缘去即成幻” 博客,谢绝转载!

你可能感兴趣的:(C语言,线性表,include)