关闭

(C语言)顺序表之动态实现

597人阅读 评论(0) 收藏 举报
分类:

点击打开链接(静态实现版)

http://blog.csdn.net/snow_5288/article/details/51520287

头文件:

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _SEQLIST_H__
#define _SEQLIST_H__

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

#define MAX 2
#define CHANGE_MAX 2
typedef int TYPE;
typedef struct Seqlist
{
   TYPE *data;//顺序表的最大容量
   int sz;//当前顺序表的容量
   int capacity;//顺序表能存储的最大容量
}Seqlist,*pSeqlist;

enum OP
{
	EXIT,
    PRINT,
	PUSH_BACK,
	POP_BACK,
	PUSH_FRONT,
	POP_FRONT,
	INSERT_POS,
	REMMOVE_POS,
	REMMOVE_ALL,
	SORT,
	BIN_SEARCH
};

void menu();//打印菜单
void check_capacity(pSeqlist seq);//检查是否需要扩容
void destroy(pSeqlist seq);//释放动态申请的空间
void init_seqlist(pSeqlist seq);//初始化顺序表
void print_seqlist(pSeqlist seq);//打印顺序表
void push_back(pSeqlist seq,TYPE x);//删除顺序表的尾部元素
void pop_back(pSeqlist seq);//在顺序表的尾部插入一个元素
void push_front(pSeqlist seq,TYPE x);//删除顺序表的首部元素
void pop_front(pSeqlist seq);//在顺序表的首部插入一个元素
void insert_pos(pSeqlist seq,TYPE pos,TYPE x);//在特定位置插入元素x
void remmove_pos(pSeqlist seq, TYPE x);//删除顺序表中的特定元素x
void remmove_all(pSeqlist seq,TYPE x);//删除所有值为x的元素
void sort(pSeqlist seq);//排序顺序表(可通过冒泡排序,快速排序,选择排序等多种排序方法)
int binary_search(pSeqlist seq,TYPE x);//二分查找顺序表中特定元素x


#endif//_SEQLIST_H__
主函数部分:

#include"seq.h"

int main()
{
	TYPE x=0;
	int input=1;
	int sz=0;
	int pos=0;
	int ret=0;
	Seqlist seq;
	init_seqlist(&seq);

	
	while(input)
	{
		menu();
		printf("请输入一个你要进行的操作:");
		scanf("%d",&input);
		switch(input)
		{
		case PRINT:
			print_seqlist(&seq);
			break;
		case PUSH_BACK:
			printf("一个你要添加的元素:");
			scanf("%d",&x);
			push_back(&seq,x);
			break;
		case POP_BACK:
			pop_back(&seq);
			break;
		case PUSH_FRONT:
			printf("一个你要添加的元素:");
			scanf("%d",&x);
			push_front(&seq,x);
			break;
		case POP_FRONT:
			pop_front(&seq);
			break;
		case INSERT_POS:
			printf("请输入你要添加的位置:");
			scanf("%d",&pos);
			printf("一个你要添加的元素:");
			scanf("%d",&x);
            insert_pos(&seq,pos,x);
			break;
		case REMMOVE_POS:
			printf("请输入你要删除的位置:");
			scanf("%d",&pos);
			remmove_pos(&seq,pos);
			break;
		case REMMOVE_ALL:
			printf("一个你要删除的元素:");
			scanf("%d",&x);
			remmove_all(&seq,x);
			break;
		case SORT:
			sort(&seq);
			break;
		case BIN_SEARCH:
			printf("一个你要查找的元素:");
			scanf("%d",&x);
			ret=binary_search(&seq,x);
			printf("你要查找的数为:%d\n",ret);
			break;
		case EXIT:
			destroy(&seq);
			seq.data=NULL;
			break;
		}
    }
	system("pause");
	return 0;
}
代码实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"seq.h"
void menu()
{
     
	 printf("********************\n");
	 printf("***1.print_seqlist**\n");
	 printf("***2.push_back******\n");
	 printf("***3.pop_back*******\n");
	 printf("***4.push_front*****\n");
	 printf("***5.pop_front******\n");
	 printf("***6.insert_pos*****\n");
	 printf("***7.remmove_pos****\n");
	 printf("***8.remmove_all****\n");
	 printf("***9.sort***********\n");
	 printf("**10.binary_search**\n");
	 printf("**0.exit***********\n");


}
void check_capacity(pSeqlist seq)//检查是否需要扩容
{
	if(seq->sz==seq->capacity)//说明人数已达到可容纳人数的最大值
	{
		TYPE *tmp=(TYPE *)realloc(seq->data,(seq->capacity+CHANGE_MAX)*sizeof(TYPE));
		if(tmp==NULL)
		{
		    printf("out of memory\n");
			exit(EXIT_FAILURE);
		}
		else
		{
			seq->data=tmp;
			seq->capacity+=CHANGE_MAX;
		}
	}
}
void init_seqlist(pSeqlist seq)//初始化顺序表
{
	seq->data=(TYPE *)malloc(MAX*sizeof(TYPE));//动态申请空间
	
	if(seq->data==NULL)
	{
	    printf("out of memory.\n");
		exit(EXIT_FAILURE);
	}
	seq->sz=0;
	seq->capacity=MAX;
	memset(seq->data,0,MAX*sizeof(TYPE));
}
void print_seqlist(pSeqlist seq)//打印顺序表
{
	int i=0;
	for(;i<seq->sz;i++)
	{
		printf("%d ",seq->data[i]);
	}
	printf("\n");
}
void push_back(pSeqlist seq,TYPE x)//顺序表的尾部插入元素x
{
	assert(seq);
	check_capacity(seq);//插入元素前先检查是否需要扩容
		seq->data[seq->sz]=x;
		seq->sz++;
}
void pop_back(pSeqlist seq)//在顺序表的尾部一个删除元素
{
	assert(seq);
	if(seq->sz == 0)
	{
		printf("顺序表已空\n");
		return ;
	}
	else
	{
		seq->sz--;
	}
}
void push_front(pSeqlist seq,TYPE x)//顺序表的首部插入元素x
{
	int i=0;
	assert(seq);
	check_capacity(seq);//插入元素前先检查是否需要扩容
	for(i=0; i<seq->sz; i++)
	{
		seq->data[seq->sz+i]=seq->data[seq->sz-1+i];
	}
	seq->data[0]=x;
	seq->sz++;
}
void pop_front(pSeqlist seq)//在顺序表的首部删除一个元素
{
	assert(seq);
	if(seq->sz==0)
	{
	   printf("顺序表已空!\n");
	   return;
	}
	else
	{
	    int i=0;
		for(i=0; i<seq->sz; i++)
		seq->data[i]=seq->data[i+1];
		seq->sz--;

	}
	
}
void insert_pos(pSeqlist seq,TYPE pos,TYPE x)//在特定位置插入元素x
{
	int i=0;
	assert(seq);
    
	check_capacity(seq);//插入元素前先检查是否需要扩容
	for(i=seq->sz-1; i>=pos; i--)
			{
				seq->data[i+1]=seq->data[i];seq->sz++;
			}
			seq->data[pos]=x;
			seq->sz++;	
}
void remmove_pos(pSeqlist seq, TYPE x)//删除顺序表中的特定元素x
{
	assert(seq);
	if(seq->sz==0)
	{
	     printf("顺序表已空!\n");
	}
	else
	{ 
	   int i=0;
	   for(i=0; i<seq->sz; i++)
	   {
		   if(seq->data[i]==x)
		   {
			   seq->data[i]=seq->data[i+1];
		   }
	   }
	   seq->sz--;
	}
}
void remmove_all(pSeqlist seq,TYPE x)//删除所有值为x的元素
{
	if(seq->sz==0)
	{
	     printf("顺序表已空!\n");
	}
	else
	{
	   int count=0;
	   int i=0;
	   assert(seq);

	   for(i=0; i<seq->sz; i++)
	   {
		   if(seq->data[i]==x)
		   {
			   count++;
			   seq->data[i]=seq->data[i+1];
		   }
	   }
	   seq->sz=seq->sz-count;
	}
}
void sort(pSeqlist seq)//排序顺序表(可通过冒泡排序,快速排序,选择排序等多种排序方法)
{
	if(seq->sz==0)
	{
	    printf("顺序表为空!\n");
	}
	else
	{
	    int i=0;
		int j=0;
		assert(seq);

		for(i=0; i<seq->sz; i++)
		{
		    for(j=0; j<seq->sz-i; j++)
			{
				if(seq->data[j]>seq->data[j+1])
				{
				   TYPE tmp=0;
				   tmp=seq->data[j];
				   seq->data[j]=seq->data[j+1];
				   seq->data[j+1]=tmp;
				}
			}
		}
	}
}
int binary_search(pSeqlist seq,TYPE x)//二分查找顺序表中特定元素x
{
	int left=0;
	int right=seq->sz-1;
	int mid=(right-left)/2+left;
	assert(seq);
	if(seq->sz==0)
	{
	    printf("顺序表为空!\n");
	}
	else
	{
		Seqlist pseq;
		sort(&pseq);//勿忘二分查找的前提必须为有序数组
		while(left<right)
		{
			if(seq->data[x]<seq->data[mid])
			{
			    right=mid-1;
			}
			else if(seq->data[x]>seq->data[mid])
			{
			    left=mid+1;
			}
			else
			{
				return seq->data[mid];
			}
			left--;
			right++;
		}
    return  seq->data[mid];
    printf("查找成功!\n");
	}
   return seq->data[mid];
}

void destroy(pSeqlist seq)//释放动态申请的空间
{
	free(seq->data);
	seq->data=NULL;
	seq->sz=0;
	seq->capacity=0;
}


review again:

头文件:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__
//#pragma once

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

typedef int Datatype;

typedef struct SeqList
{
	Datatype *arr;
	int size;
	int capacity;
}SeqList,*pseq;

enum OP
{
   EXIT,
   PRINT_SEQLIST,
   PUSHBACK,
   POPBACK,
   PUSHFRONT,
   POPFRONT,
   INSERT,
   ERASE,
   FIND,
   REMMOVE,
   REMMOVE_ALL,
   EMPTY,
   SIZE,
   BUBBLE_SORT,
   SELECT_SORT,
   BINARY_SEARCH,
   CLEAR
};
void init_seqlist(pseq seq);
void check_capacity(pseq seq);
void print_seqlist(pseq seq);
void pushback(pseq seq,Datatype x);
void popback(pseq seq);
void pushfront(pseq seq,Datatype x);
void popfront(pseq seq);
void insert(pseq seq,int pos,Datatype x);
void erase(pseq seq,int pos);
int find(pseq seq,Datatype x);
void remmove(pseq seq,Datatype x);
void remmove_all(pseq seq,Datatype x);
void empty(pseq seq);
int size(pseq seq);
void bubble_sort(pseq seq);
void select_sort(pseq seq);
//int binary_search(pseq seq,Datatype x);
int binary_search(pseq seq,int left,int right,Datatype x);
void clear(pseq seq);
void destroy(pseq seq);


#endif //__SEQLIST_H__<span style="color:#ff0000;">
</span>

测试代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include"m_seqlist.h"

void menu()
{
	printf("0.exit*************1.print_SeqList*****\n");
	printf("2.pushback*********3.pophback**********\n");
	printf("4.pushfront********5.popfront**********\n");
	printf("6.insert***********7.erase*************\n");
	printf("8.find*************9.remmove***********\n");
	printf("10.remmove_all*****11.empty************\n");
	printf("12.size************13.bubble_sort******\n");
	printf("14.select_sort*****15.binary_search****\n");
	printf("16.clear*******************************\n");
}
int main()
{
   int input = 1;
   SeqList seqlist;
   init_seqlist(&seqlist);
   while(1)
   {
	   menu();
	   printf("请输入你要操作的菜单选项:");
       scanf("%d",&input);
	   switch(input)
	   {
	   case PRINT_SEQLIST:
		   print_seqlist(&seqlist);
		   break;
	   case PUSHBACK:
		   {
			   Datatype x = 0;
			   printf("请输入你要插入的元素:");
			   scanf("%d",&x);
			   pushback(&seqlist,x);
		   }
		   break;
	   case POPBACK:
		   popback(&seqlist);
		   break;
	   case PUSHFRONT:
			{
				Datatype x = 0;
				printf("请输入你要插入的元素:");
				scanf("%d",&x);
				pushfront(&seqlist,x);
			}
		   break;
	   case POPFRONT:
		   popfront(&seqlist);
		   break;
	   case INSERT:
		   {
			   Datatype x = 0;
			   int pos = 0;
			   printf("请输入你要插入的元素:");
			   scanf("%d",&x);
			   printf("请输入你要插入的位置:");
			   scanf("%d",&pos);
			   insert(&seqlist,pos,x);
		   }
		   break;
	   case ERASE:
		   {
			   int pos = 0;
			   printf("请输入你要擦除插入的位置:");
			   scanf("%d",&pos);
			   erase(&seqlist,pos);
		   }
		   break;
	   case FIND:
		   {
			   int ret = 0;
			   Datatype x = 0;
			   printf("请输入你要查找的元素:");
			   scanf("%d",&x);
			   ret=find(&seqlist,x);
			   if(ret!=-1)
				   printf("你要查找的元素第一次出现的下标为:%d\n",ret);
			   else
				   printf("你要查找的元素不存在\n");
		   }
		   break;
	   case REMMOVE:
		   {
			   Datatype x = 0;
			   printf("请输入你要删除的元素:");
			   scanf("%d",&x);
			   remmove(&seqlist,x);
		   }
		   break;
	   case REMMOVE_ALL:
		   {
			   Datatype x = 0;
			   printf("请输入你要删除的元素:");
			   scanf("%d",&x);
			   remmove_all(&seqlist,x);
		   }
		   break;
	   case EMPTY:
		   empty(&seqlist);
		   break;
	   case SIZE:
		   {
		       int ret = size(&seqlist);
			   printf("顺序表中共有%d个元素\n",ret);
		   }
		   break;
	   case BUBBLE_SORT:
		   bubble_sort(&seqlist);
		   break;
	   case SELECT_SORT:
		   select_sort(&seqlist);
		   break;
	   case BINARY_SEARCH:
		   {
			   int ret = 0;
			   Datatype x = 0;
			   printf("请输入你要查找的元素:");
			   scanf("%d",&x);
			   //ret = binary_search(&seqlist,x);
			   ret = binary_search(&seqlist,0,seqlist.size-1,x);
			   if(ret == -1)
			       printf("没找到要查找元素\n");
			   else
				   printf("查找元素的下标为:%d\n",ret);
		   }
           break;
	   case CLEAR:
		   clear(&seqlist);
		   break;
	   case 0:
		   destroy(&seqlist);
		   exit(0);
		   break;
	   default:
		   printf("参数错误,请重新输入\n");
		   break;
	   }
   }
  
   system("pause");
   return 0;
}

函数实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"m_seqlist.h"

void init_seqlist(pseq seq)
{
    assert(seq);
	seq->arr = (Datatype *)malloc(3*sizeof(Datatype));//起初先分配3个空间
	if(NULL == seq->arr)
	{
	    printf("out of memory.\n");
		exit(1);
	}
	else
	{
	    memset(seq->arr,0,3*sizeof(Datatype));
		seq->size = 0;
		seq->capacity = 3;
	}
}

void check_capacity(pseq seq)
{
	Datatype *temp = NULL;
	assert(seq);
	if(seq->size >= seq->capacity)//说明容量不够了,需要扩容
	{
		temp = (Datatype *)realloc(seq->arr,(3+seq->capacity*2)*sizeof(Datatype));
		if(NULL == temp)
		{
			printf("out of memory.\n");
			exit(1);
		}
		else
		{
		    seq->arr = temp;
			seq->capacity += 3+seq->capacity*2;
		}
	}
}

void print_seqlist(pseq seq)
{
    int i = 0;
	assert(seq);
	for(i=0; i<seq->size; i++)
	{
		printf("%d->",seq->arr[i]);
	}
	printf("end\n");
}
void pushback(pseq seq,Datatype x)
{
	assert(seq);
    check_capacity(seq);
	seq->arr[seq->size++] = x;
	printf("插入成功\n");
}
void popback(pseq seq)
{
	assert(seq);
	if(seq->size==0)
		printf("顺序表已空\n");
	else
	{
		//方法1:
	    seq->size--;//将最后一个元素删除即可

		////方法2:调用擦除函数实现
		//erase(seq,seq->size-1);
	    printf("删除成功\n");
	}
}
void pushfront(pseq seq,Datatype x)
{
	int i = 0;
	assert(seq);
	check_capacity(seq);
	 //   //方法1(如果顺序表的元素与顺序无关)
		//seq->arr[seq->size]=seq->arr[0];//将首元素保存到最后位置
		//seq->arr[0]=x;
		//seq->size++;

		////方法2:调用插入函数
		//insert(seq,0,x);

		////方法3:
		//int i = 0;
		//for(i=seq->size; i>0; i--)//移动元素
		//{ 
		//	seq->arr[i] = seq->arr[i-1];
		//}
		//seq->arr[0] = x;//插入元素
		//seq->size++;

		//方法4:
	for(i=seq->size-1; i>=0; i--)//移动元素
	{ 
		seq->arr[i+1] = seq->arr[i];
	}
	seq->arr[0] = x;//插入元素
	seq->size++;
	printf("插入成功\n");
}
void popfront(pseq seq)
{
	assert(seq);
	if(seq->size == 0)
		printf("顺序表已空\n");
	else
	{
		//   //方法1(如果顺序表的元素与顺序无关)
		//seq->arr[0]=seq->arr[seq->size-1];//将首元素保存到最后
		//seq->size--;

		////方法2:直接调用擦除函数
		//erase(seq,0);

		////方法3:
		//int i = 0;
		//for(i=0; i<seq->size-1; i++)//移动元素
		//{ 
		//	seq->arr[i] = seq->arr[i+1];
		//}
		//seq->size--;

		//方法4:
		int i = 0;
		for(i = 1; i<seq->size; i++)//移动元素
		{ 
			seq->arr[i-1] = seq->arr[i];
		}
		seq->size--;
	}   
	printf("删除成功\n");
}
void insert(pseq seq,int pos,Datatype x)
{
	int i = 0;
	assert(seq);
	check_capacity(seq);
		////方法1:
	 //   int i = 0;
		//for(i=seq->size; i>pos; i--)
		//{
		//	seq->arr[i] = seq->arr[i-1];
		//}
		//seq->arr[pos] = x;
		//seq->size++;

		//方法2:
		for(i=seq->size-1; i>=pos; i--)
		{
			seq->arr[i+1] = seq->arr[i];
		}
		seq->arr[pos] = x;
		seq->size++;
	printf("插入成功\n");
}
void erase(pseq seq,int pos)
{
	assert(seq);
	if(seq->size==0)
		printf("顺序表已空\n");
	else
	{
	    int i = 0;
		for(i=pos; i<seq->size-1; i++)
		{
			seq->arr[i] = seq->arr[i+1];
		}
		seq->size--;
		printf("擦除成功\n");
	}
}
int find(pseq seq,Datatype x)
{
	int i = 0;
	assert(seq);
	for(i=0; i<seq->size; i++)
	{
		if(seq->arr[i] == x)
			return i;//返回首次出现位置的下标
	}
	return -1;
}
void remmove(pseq seq,Datatype x)
{
	assert(seq);
	if(seq->size==0)
		printf("顺序表已空\n");
	else
	{
		int i = 0;
	    for(i=0; i<seq->size; i++)
		{
			if(seq->arr[i]==x)
			{
				int j = 0;
				for(j=i; j<seq->size; j++)//移动元素
				{
					seq->arr[j]=seq->arr[j+1];
				}
			}
		}
		seq->size--;
		printf("删除成功\n");
	}

}
void remmove_all(pseq seq,Datatype x)
{
	assert(seq);
	if(seq->size==0)
		printf("顺序表已空\n");
	else
	{
	 //   //方法1:找一个删一个
		//int i = 0;
		//int count = 0;
		//for(i=0; i<seq->size; ++i)
		//{
		//	if(seq->arr[i] == x)
		//	{
		//		int j = 0;
		//		for(j=i; j<seq->size; j++)//移动元素
		//		{
		//			seq->arr[j]=seq->arr[j+1];
		//		}
		//		seq->size--;
		//	}
		//}

		//方法2:
		int i = 0;
		int count = 0;
		assert(seq);
		for(i=0; i<seq->size; i++)
		{
			seq->arr[i-count]=seq->arr[i];
			if(seq->arr[i] == x)
				count++;
		}
		seq->size-=count;
	}
		printf("删除成功\n");
}
void empty(pseq seq)
{
	assert(seq);
    if(seq->size == 0)
		printf("顺序表为空\n");
	else
		printf("顺序表不为空\n");
}

int size(pseq seq)
{
    assert(seq);
	return seq->size;
}
void bubble_sort(pseq seq)
{
 //   //普通写法
	//int i = 0;
	//int flag = 0;
	//assert(seq);
	//for(i=0; i<seq->size-1; i++)
	//{
	//    int j = 0;
	//	flag = 0;
	//	for(j=0; j<seq->size-1-i; j++)
	//	{
	//		if(seq->arr[j]>seq->arr[j+1])//升序
	//		{
	//			Datatype temp = seq->arr[j];
	//			seq->arr[j] = seq->arr[j+1];
	//			seq->arr[j+1] = temp;
	//			flag = 1;
	//		}
	//	}
	//	if(flag == 0)
	//		break;
	//}

	//自由选择升序或排序
	int option = 0;
	int i = 0;
	int flag = 0;
	assert(seq);
	printf("请输入你的选择:(0.升序  1.降序)");
	scanf("%d",&option);
	for(i=0; i<seq->size-1; i++)
	{
	    int j = 0;
		flag = 0;
		for(j=0; j<seq->size-1-i; j++)
		{
			if(option == 0)
			{
			    if(seq->arr[j]>seq->arr[j+1])//升序
				{
					Datatype temp = seq->arr[j];
					seq->arr[j] = seq->arr[j+1];
					seq->arr[j+1] = temp;
					flag = 1;
				}
			}
			else if(option == 1)
			{
			    if(seq->arr[j]<seq->arr[j+1])//降序
				{
					Datatype temp = seq->arr[j];
					seq->arr[j] = seq->arr[j+1];
					seq->arr[j+1] = temp;
					flag = 1;
				}
			}
		}
		if(flag == 0)
			break;
	}
	printf("排序成功\n");
}

void select_sort(pseq seq)
{
	;
}

//int binary_search(pseq seq,Datatype x)//假设顺序表已经按升序排列好
//{
//	int left = 0;
//	int right = 0;
//	int mid = 0;
//	assert(seq);
//	////方法1,前闭后闭区间
//	/*right = seq->size-1;
//	while(left <= right)
//	{
//	    mid = left + ((right - left)>>1);
//		if(seq->arr[mid] > x)
//			right = mid-1;
//		else if(seq->arr[mid] < x)
//			left = mid+1;
//		else
//			return mid;
//	}
//	return -1;*/
//
//	////方法2:前闭后开区间
//	//right = seq->size;
//	//while(left < right)
//	//{
//	//    mid = (left & right)+((left ^ right)>>1);
//	//	if(seq->arr[mid] > x)
//	//		right = mid;
//	//	else if(seq->arr[mid] < x)
//	//		left = mid+1;
//	//	else
//	//		return mid;
//	//}
//	//return -1;
//}
//方法3:递归实现
int binary_search(pseq seq,int left,int right,Datatype x)//假设顺序表已经按升序排列好
{
    int mid = 0;
	assert(seq);
    if(left > right)
		return -1;
	mid = left + ((right - left)>>1);
	if(seq->arr[mid] > x)
		return binary_search(seq,left,mid-1,x);
	else if(seq->arr[mid] < x)
		return binary_search(seq,mid+1,right,x);
	else
		return mid;
}

void clear(pseq seq)
{
	assert(seq);
	seq->size = 0;
}
void destroy(pseq seq)
{
	assert(seq);
	free(seq->arr);
	seq->arr = NULL;
}



3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:181265次
    • 积分:3092
    • 等级:
    • 排名:第11153名
    • 原创:119篇
    • 转载:4篇
    • 译文:1篇
    • 评论:73条
    博客专栏
    文章分类
    最新评论
    C专区
    一门流行40余年的编程语言,你,值得拥有!