1. 程式人生 > >C語言實現線性表基本操作

C語言實現線性表基本操作

style eal struct fine fin delete class logs destroy

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

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10


// TODO: 在此處引用程序需要的其他頭文件
//
typedef struct{
int *elem;
int length;//當前長度
int listsize;//當前分配的存儲容量
}SqList;

//新建線性表
bool Init_List(SqList* L){
L->elem = (int*)malloc(LIST_INIT_SIZE*sizeof
(int)); if (!L->elem) return false; L->length = 0; L->listsize = LIST_INIT_SIZE; return true; } //銷毀線性表 bool Destroy_List(SqList* L){ if (L){ free(L); L = NULL; return true; } else return false; } //表末新增一個元素 bool Append_List(SqList* L,int value){ if (L->length >= L->listsize){ L->elem = (int
*)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(int)); L->listsize += LISTINCREMENT; } L->length++; L->elem[L->length] = value; return true; } //打印線性表 void Print_List(SqList L){ for (int i = 1; i<=L.length;i++){ printf("%d", L.elem[i]); } } //指定位置locate插入值valu bool Insert_List(SqList* L, int
locate, int value){ if (L->length >= L->listsize){ L->elem = (int*)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(int)); L->listsize += LISTINCREMENT; } for (int i = L->length; i >= locate; i--){ L->elem[i + 1] = L->elem[i]; } L->elem[locate] = value; L->length++; return true; } //刪除指定位置locate元素 bool Delete_List(SqList* L, int locate){ for (int i = locate; i<L->length; i++){ L->elem[i] = L->elem[i + 1]; } L->elem[L->length] = NULL; L->length--; return true; } //清空線性表 bool Clear_List(SqList* L){ while (L->length){ L->elem[L->length] = NULL; L->length--; } return true; }

C語言實現線性表基本操作