데이터 구조의 C 언어 구현 순서 표 (SeqList) 주 함수 없 음

5201 단어
#include
#include
#include
#define ElemType int
#define SEQLIST_DEFAULT_SIZE 8
#define SEQLIST_INC_SIZE     5
//        
typedef struct SeqList
{
	ElemType *base; //int *base
	size_t    capacity;//     
	size_t    len;//     (  )
}SeqList;


void InitSeqList(SeqList *list);//   
void ShowSeqList(SeqList *list);//  
bool push_back(SeqList *list, ElemType x);//  
bool push_front(SeqList *list, ElemType x);//  
size_t Length(SeqList *list);//   
bool insert_pos(SeqList *list, int pos, ElemType x);//     
bool pop_back(SeqList *list);//  
bool pop_front(SeqList *list);//  
bool insert_val(SeqList *list, ElemType x);//    
bool delete_pos(SeqList *list, int pos);//     
bool delete_val(SeqList *list, ElemType key);//    
int  find_key(SeqList *list, ElemType key);//     
void reverse_list(SeqList *list);//     
void sort_list(SeqList *list);//  
void clear_list(SeqList *list);//  
void destroy_list(SeqList *list);//  


bool IsFull(SeqList *list)//  
{
	return list->len >= list->capacity;
}
bool IsEmpty(SeqList *list)//  
{
	return list->len == 0;
}
void Swap(ElemType &a, ElemType &b)//   
{
	ElemType tmp = a;
	a = b;
	b = tmp;
}
//     
bool Inc(SeqList *list)
{
	size_t new_capacity = list->capacity+SEQLIST_INC_SIZE;
	ElemType *new_base = (ElemType*)realloc(list->base, sizeof(ElemType)*new_capacity);
	if(new_base == NULL)
		return false;
	list->capacity = new_capacity;
	list->base = new_base;
	return true;
}


void InitSeqList(SeqList *list)
{
	list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_DEFAULT_SIZE);//     
	assert(list->base != NULL);//  ,            
	list->capacity = SEQLIST_DEFAULT_SIZE;
	list->len = 0;
}
void ShowSeqList(SeqList *list)
{
	for(int i=0; ilen; ++i)
	{
		printf("%d",list->base[i]);
	}
	printf("
"); } bool push_back(SeqList *list, ElemType x) { if(list->len>=list->capacity && !Inc(list)) //if(!Inc(list) && list->len>=list->capacity) { printf(" , "); return false; } list->base[list->len++] = x; //list->len++; return true; } bool push_front(SeqList *list, ElemType x) { if(list->len >= list->capacity) { printf(" ,
"); return false; } for(int i=list->len; i>0; --i) { list->base[i] = list->base[i-1]; } list->base[0] = x; list->len++; return true; } size_t Length(SeqList *list) { return list->len; } bool insert_pos(SeqList *list, int pos, ElemType x) { if(list->len >= list->capacity) { printf(" ,
"); return false; } if(pos<0 || pos>list->len) { printf("
"); return false; } for(int i=list->len; i>pos; --i) { list->base[i] = list->base[i-1]; } list->base[pos] = x; list->len++; return true; } bool pop_back(SeqList *list) { if(list->len == 0) { printf(" ,
"); return false; } list->len--; return true; } bool pop_front(SeqList *list) { if(list->len == 0) { printf(" ,
"); return false; } for(int i=0; ilen-1; ++i) { list->base[i] = list->base[i+1]; } list->len--; return true; } bool insert_val(SeqList *list, ElemType x) { if(list->len >= list->capacity) { printf(" ,
"); return false; } int i,j; for(i=0; ilen; ++i) { if(x < list->base[i]) { for(j=list->len; j>i; --j) { list->base[j] = list->base[j-1]; } break; } } list->base[i] = x; list->len++; return true; } bool delete_pos(SeqList *list, int pos) { if(list->len == 0) { printf(" ,
"); return false; } if(pos<0 || pos>=list->len) { printf(" ,
"); return false; } for(int i=pos; ilen-1; ++i) { list->base[i] = list->base[i+1]; } list->len--; return true; } bool delete_val(SeqList *list, ElemType key) { if(list->len == 0) { printf(" ,
"); return false; } int del_pos = find_key(list, key); if(del_pos == -1) { printf("
"); return false; } return delete_pos(list, del_pos); } int find_key(SeqList *list, ElemType key) { for(int i=0; ilen; ++i) { if(key == list->base[i]) return i; } return -1; } void reverse_list(SeqList *list) { if(list->len > 1) { int low = 0; int high = list->len-1; while(low < high) { Swap(list->base[low], list->base[high]); low++; high--; } } } void sort_list(SeqList *list) { if(list->len > 1) { for(int i=0; ilen-1; ++i) { for(int j=0; jlen-i-1; ++j) { if(list->base[j] > list->base[j+1]) { Swap(list->base[j], list->base[j+1]); } } } } } void clear_list(SeqList *list) { list->len = 0; } void destroy_list(SeqList *list) { free(list->base); list->base = NULL; // list->capacity = list->len = 0; }

좋은 웹페이지 즐겨찾기