最近想用C把數據結構刷一遍,事實上當初學也是隻理解了皮毛,只有實際動手纔能有深刻的體會,閒話少說,直接上碼。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIST_INIT_SIZE 100 //順序表存儲空間的初始分配量
#define LISTINCREAMENT 10 //順序表存儲空間的分配增量
typedef struct students
{
char sno[5]; //學號
char name[20]; //姓名
char sex[3]; //性別
int score; //分數
}ElemType;
typedef struct
{
ElemType *elem; //存儲空間基址
int length; //當前長度
int listsize; //當前分配的存儲容量(以sizeof(ElemType)爲單位)
}SqList;
SqList * InitSqList(); // 構造空的順序表
int InsertSqList(SqList *l, int n, ElemType *e); // 在順序表第 n 個位置之前插入新元素 e
int AppendSqList(SqList *l, ElemType *e); // 在末尾追加元素e
bool IsEmpty(SqList *l); // 判斷是否爲空
bool IsFull(SqList *l); // 判斷是否爲滿
void FreeSqList(SqList *l); // 釋放順序表
int DeleteSqList(SqList *l, int n, ElemType *e); // 在順序表中刪除第 n 個元素,並用 e 返回其值
void PrintSqList(SqList *l); // 打印
ElemType stu[4] =
{
{"S101","張三","男",80},
{"S102","李四","女",75},
{"S103","王五","男",90},
{"S104","笨蛋","女",85}
};
int main()
{
int i;
SqList * list = InitSqList();
ElemType t;
if (IsEmpty(list))
{
printf("list is empty\n\n");
}
for (i = 0; i < 3; ++i)
{
InsertSqList(list,i + 1,&stu[i]);
}
PrintSqList(list);
printf("\n\n");
AppendSqList(list,&stu[3]);
PrintSqList(list);
printf("\n\n");
DeleteSqList(list,2,&t);
PrintSqList(list);
printf("\n\n");
FreeSqList(list);
PrintSqList(list);
return 0;
}
// 構造空的順序表
SqList * InitSqList()
{
SqList * l = (SqList *)malloc(sizeof(SqList));
if (NULL == l)
{
exit(0);
}
l->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (NULL == l->elem)
{
exit(0);
}
l->length = 0;
l->listsize = LIST_INIT_SIZE;
return l;
}
// 在順序表第n個位置之前插入新元素e
int InsertSqList(SqList *l, int n, ElemType *e)
{
ElemType *newelem = NULL, *t = NULL, *p = NULL;
int i;
if (NULL == l || n < 1 || n > l->length + 1 || NULL == e)
{
return 0;
}
if (IsFull(l))
{
newelem = (ElemType *)realloc(l->elem, (l->listsize + LISTINCREAMENT) * sizeof(ElemType));
l->elem = newelem;
l->listsize += LISTINCREAMENT;
}
// t = &l->elem[n - 1];
for (i = l->length - 1; i >= n - 1; --i) //插入位置及之後的元素右移,下面那種實現也可以
{
l->elem[i] = l->elem[i - 1];
}
// for(p = &(l->elem[l->length-1]); p >= t; p--)
// {
// *(p+1) = *p; //插入位置及之後的元素右移
// }
// *t = *e;
l->elem[n - 1] = *e;
++l->length;
return 1;
}
// 在末尾追加元素e
int AppendSqList(SqList *l, ElemType *e)
{
ElemType *newelem = NULL, *t = NULL;
if (NULL == l || NULL == e)
{
return 0;
}
if (IsFull(l))
{
newelem = (ElemType *)realloc(l->elem,(l->listsize + LISTINCREAMENT) * sizeof(ElemType));
if (NULL == newelem)
{
return 0;
}
l->elem = newelem;
l->listsize += LISTINCREAMENT;
}
// t = &l->elem[l->length];
// *t = *e;
l->elem[l->length] = *e;
++l->length;
return 1;
}
// 判斷是否爲空
bool IsEmpty(SqList *l)
{
if (0 == l->length)
{
return true;
}
else
{
return false;
}
}
// 判斷是否爲滿
bool IsFull(SqList *l)
{
if (LIST_INIT_SIZE == l->length)
{
return true;
}
else
{
return false;
}
}
// 釋放順序表
void FreeSqList(SqList *l)
{
free(l->elem);
free(l);
}
// 在順序表中刪除第 n 個元素,並用 e 返回其值
int DeleteSqList(SqList *l, int n, ElemType *e)
{
int i;
if (NULL == l || n < 1 || n > l->length || NULL == e)
{
return 0;
}
*e = l->elem[n - 1];
for (i = n - 1; i < l->length; ++i)
{
l->elem[i] = l->elem[i + 1];
}
--l->length;
return 1;
}
// 打印
void PrintSqList(SqList *l)
{
int i;
if (IsEmpty(l))
{
printf("list is empty !\n");
}
else
{
for (i = 0; i < l->length; ++i)
{
printf("%s\t%s\t%s\t%d\n",l->elem[i].sno,l->elem[i].name,l->elem[i].sex,l->elem[i].score);
}
}
}