【資料結構】實現順序表(c語言)

zhaoyaqian552發表於2015-05-19

標頭檔案:


#ifndef _SEQLIST_H
#define _SEQLIST_H

#include <stdio.h>

#define INIT_SIZE 8

typedef struct SeqList
{
	int *base;
	size_t size;
	size_t capacity;
}SeqList;

// 要實現的函式
void InitList(SeqList *list);
int isfull(SeqList *list);
int isempty(SeqList *list);

void show_list(SeqList *list);
void tail_insert(SeqList *list,int x);
void head_insert(SeqList *list,int x);
void sort(SeqList *list);
void head_delete(SeqList *list);
void tail_delete(SeqList *list);
void pos_insert(SeqList *list,int x,int pos);
void val_insert(SeqList *list, int x);
void pos_delete(SeqList *list,int pos);
int find(SeqList *list,int x);
void val_delete(SeqList *list,int x);
int length(SeqList *list);
void reverse(SeqList *list);
void clear(SeqList *list);
void destroy(SeqList *list);
void quit_system(SeqList *list,int* x);

#endif



fun:



#include "SeqList.h"

// 定義兩個返回值接收判斷函式的結果
int fret = isfull;
int eret = isempty;

// 初始化順序表
void InitList(SeqList *list)
{
	list->size = 0;
	list->capacity = INIT_SIZE;
	list->base = (int*)malloc(sizeof(int)*list->capacity);
}

// 判斷順序表是否滿
int isfull(SeqList *list)
{
	if (list->size >= list->capacity)
	{
		return -1;
	}
	else
		return 0;
}

// 判斷順序表是否空
int isempty(SeqList *list)
{
	if (list->size == 0)
	{
		return -1;
	}
	else
		return 0;
}

// 顯示
void show_list(SeqList *list)
{
	int i;
	if (list->size == 0)
	{
		printf("the table is empty!\n");
	}
	for (i = 0; i < list->size; ++i)
	{
		printf("%d  ", list->base[i]);
	}
	printf("\n");
}

// 尾插
void tail_insert(SeqList *list,int x)
{
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	list->base[list->size] = x;
	list->size++;
}

// 頭插
void head_insert(SeqList *list,int x)
{
	int i;
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	for (i = list->size; i > 0; --i)
	{
		list->base[i] = list->base[i - 1];
	}
	list->base[0] = x;
	list->size++;
}

// 排序
void sort(SeqList *list)
{
	int i;
	int j;
	int temp;
	for (i = 1; i < list->size; ++i)
	{
		for (j = 0; j < list->size - i; ++j)
		{
			if (list->base[j]>list->base[j + 1])
			{
				temp = list->base[j];
				list->base[j] = list->base[j + 1];
				list->base[j + 1] = temp;
			}
		}
	}
}

// 頭刪
void head_delete(SeqList *list)
{
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	for (i = 0; i < list->size; ++i)
	{
		list->base[i] = list->base[i + 1];
	}
	list->size--;
}

// 尾刪
void tail_delete(SeqList *list)
{
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	list->size--;
}

// 按位插入
void pos_insert(SeqList *list,int x,int pos)
{
	int i;
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	if (pos<0 || pos>list->size)
	{
		printf("the position is illegal!\n");
		return;
	}
	else
	{
		for (i = list->size; i > pos; --i)
		{
			list->base[i] = list->base[i - 1];
		}
		list->base[pos] = x;
		list->size++;
	}
}

// 按值插入
void val_insert(SeqList *list,int x)
{
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	tail_insert(list,x);
	sort(list);
}

// 按位刪除
void pos_delete(SeqList *list,int pos)
{
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	if (pos<0 || pos>list->size)
	{
		printf("the position is illegal!\n");
		return;
	}
	else
	{
		for (i = pos; i < list->size; ++i)
		{
			list->base[i] = list->base[i + 1];
		}
		list->size--;
	}
}

// 查詢
int find(SeqList *list,int x)
{
	int i;
	for (i = 0; i < list->size; ++i)
	{
		if (list->base[i] == x)
		{
			return i;
		}
	}
	printf("the number is not exit!\n");
	return 0;
}

// 按值刪除
void val_delete(SeqList *list,int x)
{
	int ret = find(list, x);
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	if (ret == -1)
	{
		printf("the number is not exist!\n");
		return;
	}
	else
	{
		for (i = ret; i <list->size; ++i)
		{
			list->base[i] = list->base[i + 1];
		}
		list->size--;
	}
}

// 求長度
int length(SeqList *list)
{
	return list->size;
}

// 反轉
void reverse(SeqList *list)
{
	int temp;
	int i = 0;
	int j = list->size - 1;
	if (eret == -1)
	{
		printf("the table is empty,can not operate!\n");
		return;
	}
	while (i < j)
	{
		temp = list->base[i];
		list->base[i] = list->base[j];
		list->base[j] = temp;
		++i;
		--j;
	}
}

// 清空
void clear(SeqList *list)
{
	list->size = 0;
}

// 摧毀
void destroy(SeqList *list)
{
	list->base = NULL;
}

// 退出系統
void quit_system(SeqList *list,int *x)
{
	*x = 0;
}


主函式:



// c實現順序表

#include "SeqList.h"

int main()
{
	SeqList mylist;
	InitList(&mylist);
	int input = 1;
	int insert = 0;
	int pos = 0;
	while (input)
	{
		printf("*********************************************************************\n");
		printf("*     [1] show_list                       [2] tail_insert           *\n"); 
		printf("*     [3] head_insert                     [4] sort                  *\n"); 
		printf("*     [5] head_delete                     [6] tail_delete           *\n"); 
		printf("*     [7] pos_insert                      [8] val_insert            *\n"); 
		printf("*     [9] pos_delete                      [10] find                 *\n"); 
		printf("*     [11] val_delete                     [12] length               *\n"); 
		printf("*     [13] reverse                        [14] clear                *\n"); 
		printf("*     [15] destroy                        [16] quit_system          *\n"); 
		printf("*********************************************************************\n"); 
		printf("please enter your choose:");
		scanf_s("%d", &input);
		switch (input)
		{
			case 1:
				show_list(&mylist);
				break;
			case 2:
				printf("please enter the number want to insert:\n");
				while (scanf_s("%d", &insert),insert != -1)
				{
					tail_insert(&mylist, insert);
				}
				break;
			case 3:
				printf("please enter the number want to insert:\n");
				while (scanf_s("%d", &insert), insert != -1)
				{
					head_insert(&mylist, insert);
				}
				break;
			case 4:
				sort(&mylist);
				break;
			case 5:
				head_delete(&mylist);
				break;
			case 6:
				tail_delete(&mylist);
				break;
			case 7:
				printf("please enter the number want to insert:\n");
				scanf_s("%d", &insert);
				printf("please enter the position :\n");
				scanf_s("%d", &pos);
				pos_insert(&mylist, insert, pos);
				break;
			case 8:
				printf("please enter the number want to insert:\n");
				scanf_s("%d", &insert);
				val_insert(&mylist, insert);
				break;
			case 9:
				printf("please enter the position :\n");
				scanf_s("%d", &pos);
				pos_delete(&mylist, pos);
				break;
			case 10:
				printf("please enter the number want to find:\n");
				scanf_s("%d", &insert);
				printf("at the %d\n", find(&mylist, insert));
				break;
			case 11:
				printf("please enter the number want to delete:\n");
				scanf_s("%d", &insert);
				val_delete(&mylist, insert);
				break;
			case 12:
				printf("the table's length is %d\n", length(&mylist));
				break;
			case 13:
				reverse(&mylist);
				break;
			case 14:
				clear(&mylist);
				break;
			case 15:
				destroy(&mylist);
				break;
			case 16:
				quit_system(&mylist, &input);
				break;
			default:
				break;
		}
	}
	return 0;
}

顯示:




尾插:




頭插:




排序:




頭刪:




尾刪:




定位插入:




按值插入:




按位刪除:




查詢:




按值刪除:




求長度:




反轉:




清除:




退出系統:





相關文章