头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#ifndef SEQLIST_H__
#define SEQLIST_H__
#define MAX 100
typedef int DataType;
typedef struct SeqList
{
DataType arr[MAX];
int count;
}SeqList,*pList;
void InItSeqList(pList p);
void PushBack(pList p,DataType data);
void PushFront(pList p, DataType data);
void PopBack(pList p);
void PopFront(pList p);
void Remove(pList p,DataType data);
void RemoveAll(pList p,DataType data);
void BubbleSort(pList p);
int Search(pList p,DataType data);
void Show(pList p);
int BinarySearch(pList p,DataType d);
#endif // !SEQLIST_H__
测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{
printf ("*******1.PushBack-尾插 2.PushFront-头插*******\n" );
printf ("*******3.PopBack-尾删 4.PopFront-头删********\n" );
printf ("*******5.Search-查找 6.Remove-删除指定元素**\n" );
printf ("*******7.RemoveAll-删除指定所有元素 8.BubbleSort-排序******\n" );
printf ("*******9.Show-显示顺序表 10.BinarySearch-二分查找*\n" );
printf ("******* **************** 0.Exit*****************\n" );
}
int main()
{
SeqList mylist;
int input = 0 ;
int data = 0 ;
InItSeqList(&mylist);
do
{
menu();
printf ("请选择需要的操作:\n" );
scanf ("%d" , &input);
switch (input)
{
case 1 :
printf ("请输入要插入的元素:" );
scanf ("%d" , &data);
PushBack(&mylist,data);
break ;
case 2 :
printf ("请输入要插入的元素:" );
scanf ("%d" , &data);
PushFront(&mylist, data);
break ;
case 3 :
PopBack(&mylist);
break ;
case 4 :
PopFront(&mylist);
break ;
case 5 :
{
int Ret = 0 ;
printf ("请输入要查找的元素:" );
scanf ("%d" , &data);
Ret = Search(&mylist, data);
if (Ret == -1 )
{
printf ("查找的元素不存在" );
}
else
{
printf ("查找的元素下标为%d\n" , Ret);
}
}
break ;
case 6 :
printf ("请输入要删除的元素:" );
scanf ("%d" , &data);
Remove(&mylist,data);
break ;
case 7 :
printf ("请输入要删除的元素:" );
scanf ("%d" , &data);
RemoveAll(&mylist,data);
break ;
case 8 :
BubbleSort(&mylist);
break ;
case 9 :
Show(&mylist);
break ;
case 10 :
printf ("请输入要查找的元素:" );
scanf ("%d" , &data);
BinarySearch(&mylist,data);
defult:
break ;
}
} while (input);
system("pause" );
return 0 ;
}
主要函数的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void InItSeqList(pList p)
{
p->count = 0 ;
memset(p->arr, 0 , sizeof(DataType)*MAX);
}
void PushBack(pList p,DataType data)
{
assert(p != NULL);
if (MAX == p->count )
{
printf("顺序表已满\n" );
return ;
}
else
{
p->arr[p->count ] = data;
p->count ++;
}
}
void PopBack(pList p)
{
if (p != NULL)
{
p->count --;
}
else
{
return ;
}
}
void PushFront(pList p, DataType data)
{
int i = 0 ;
assert(p != NULL);
if (MAX == p->count )
{
printf("顺序表已满\n" );
return ;
}
else
{
for (i = p->count ; i >0 ; i--)
{
p->arr[i] = p->arr[i - 1 ];
}
p->arr[0 ] = data;
p->count ++;
}
}
void PopFront(pList p)
{
int i = 0 ;
assert(p != NULL);
if (p->count == 0 )
{
printf("顺序表为空\n" );
}
else
{
for (i = 0 ; i < p->count -1 ; i++)
{
p->arr[i] = p->arr[i + 1 ];
}
p->count --;
}
}
void Show(pList p)
{
int i = 0 ;
assert(p != NULL);
for (i = 0 ; i < p->count ; i++)
{
printf("%d\n" , p->arr[i]);
}
}
int Search(pList p,DataType d)
{
int i = 0 ;
assert(p != NULL);
for (i = 0 ; i < p->count ; i++)
{
if (p->arr[i] == d)
{
return i;
}
else
{
return -1 ;
}
}
}
void Remove(pList p,DataType d)
{
int i = 0 ;
int pos = 0 ;
assert(p != NULL);
if (pos = Search(p, d) != -1 )
{
for (i = pos; i < p->count - 1 ; i++)
{
p->arr[i] = p->arr[i + 1 ];
}
p->count --;
}
else
{
printf("找不到需要删除的元素\n" );
return ;
}
}
void RemoveAll(pList p,DataType d)
{
int i = 0 ;
int pos = 0 ;
assert(p != NULL);
while ((pos = Search(p, d)) != -1 )
{
for (i = pos; i < p->count - 1 ; i++)
{
p->arr[i] = p->arr[i + 1 ];
}
p->count --;
}
}
void BubbleSort(pList p)
{
int i = 0 ;
int j = 0 ;
assert(p != NULL);
for (i = 0 ; i < p->count - 1 ; i++)
{
for (j = 0 ; j < p->count - 1 - i; j++)
{
if (p->arr[j]>p->arr[j+1 ])
{
DataType tmp = p->arr[j];
p->arr[j] = p->arr[j + 1 ];
p->arr[j + 1 ] = tmp;
}
}
}
}
int BinarySearch(pList p,DataType d)
{
int left = 0 ;
int right = p->count - 1 ;
while (left <= right)
{
int mid = (left - (left - right)) >> 1 ;
if (p->arr[mid] > d)
{
right = mid - 1 ;
}
else if (p->arr[mid] < d)
{
left = mid + 1 ;
}
else
{
return mid;
}
}
}