C语言手写线性表 顺序存储结构、链式存储结构
C语言手写线性表(顺序存储结构、链式存储结构)
顺序存储结构(通过数组实现)
#include<iostream>
using namespace std;
#define MaxSize 100
typedef int DataType;
typedef struct
{
DataType data[MaxSize];
int length;
}SeqList;
//初始化顺序表
void InitList(SeqList *L) {
L->length = 0;
}
//创建顺序表
int CreateList(SeqList *L, DataType a[], int n) {
if(n > MaxSize)
{
printf("You didn't have enough space to store it\n");
return 0;
}
for(int i = 0; i < n; i++)
L->data[i] = a[i];
L->length = n;
return 1;
}
//判断顺序表是否为空
int Empty(SeqList *L) {
if(L->length == 0)
return 1;
else
return 0;
}
//求顺序表的长度
int Length(SeqList *L) {
return L->length;
}
//遍历打印顺序表中元素
void PrintList(SeqList *L) {
for(int i = 0; i < L->length; i++)
printf(" %d ", L->data[i]);
printf("\n");
}
//按值查找元素
int Locate(SeqList *L, DataType x) {
for (int i = 0; i < L->length; i++)
{
if(x == L->data[i])
return i + 1; //返回元素序号
}
return 0;
}
//按位置查找
int Get(SeqList *L, int i, DataType *ptr) {
if(i < 1 || i > L->length) {
printf(" Error Location, Finding Error! \n");
return 0;
} else {
*ptr = L->data[i - 1];
return 1;
}
}
//插入元素操作
int Insert(SeqList *L, int i, DataType x) {
if(L->length >= MaxSize) {
printf("上溢错误,插入失败! \n");
return 0;
}
if(i < 1 || i > L->length + 1) {
printf("位置错误,插入失败! \n");
return 0;
}
for(int j = L->length; j >= i; j--) {
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = x;
L->length++;
return 1;
}
//删除元素操作
int Delete(SeqList *L, int i, DataType *ptr) {
if(L->length == 0) {
printf("下溢错误,删除失败! \n");
return 0;
}
if(i < 1 || i > L->length) {
printf("位置错误,删除失败! \n");
return 0;
}
*ptr = L->data[i - 1];
for(int j = i; j < L->length; j++) {
L->data[j - 1] = L->data[j];
}
L->length--;
return 1;
}
//主函数
int main() {
int r[5] = {1, 2, 3, 4, 5}, i, x;
SeqList L;
CreateList(&L, r, 5); //建立具有5个元素的顺序表
printf("Print Current List: \n");
PrintList(&L); //打印当前顺序表 1,2,3,4,5
Insert(&L, 2, 8); //在位置2插入元素8
printf("After Inserting , Current List is : \n");
PrintList(&L); //打印顺序表 1,8,2,3,4,5
printf("Print the length of current Seqlist : %d \n", Length(&L)); //打印长度 6
printf("Please Input the value you want to find: \n");
scanf("%d", &x);
i = Locate(&L, x); //定位想查询的元素位置,通过元素值查询位置
if(i == 0)
printf("Didn't Found! \n");
else
printf("the value (%d) you want to find is located in : %d \n", x, i);
printf("please input the location you want to find : \n"); //通过位置查询元素值
scanf("%d", &i);
if(Get(&L, i, &x) == 1)
printf("%d in L[] is %d. \n", i, x);
else
printf("Didn't found! \n");
printf("Please input the location you want to delete: \n"); //通过位置删除元素
scanf("%d", &i);
if(Delete(&L, i, &x) == 1)
{
printf("delete L[%d] is %d, Now the elements is : \n ", i, x);
PrintList(&L);
}
else printf("delete error!\n");
system("pause");
return 0;
}
编译运行结果:
链式存储结构(结构体实现)
#include<iostream>
using namespace std;
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node * next;
}Node;
//单链表初始化
Node * InitList() {
Node * first = (Node *) malloc(sizeof(Node)); //生成头结点
first->next = NULL; //头结点指针域设置为空
return first;
}
//判断链表是否为空
int Empty(Node * first) {
if(first->next == NULL) //单链表为空,返回1
return 1;
else //否则非空返回0
return 0;
}
//遍历输出单链表中元素
void PrintList(Node * first) {
Node * p = first->next; //工作指针p初始化
while(p != NULL) {
printf(" %d ->", p->data); //输出节点数据域
p = p->next; //工作指针后移,注意不能写作p++(因为存储位置不连续)
}
printf("\n");
}
//求链表长度
int Length(Node * first) {
Node *p = first->next; //工作指针初始化
int count = 0; //累加器初始化
while(p != NULL) {
p = p->next;
count++;
}
return count;
}
//按值查找位置
int Locate(Node * first, DataType x) {
Node * p = first->next; //工作指针初始化
int count = 1; //累加器初始化
while(p != NULL) {
if(p->data == x) { //查找成功,返回所在位置
return count;
}
p = p->next;
count++;
}
return 0; //否则返回0
}
//按照位置查找值
int Get(Node * first, int i, DataType *ptr) {
Node *p = first->next; //工作指针初始化
int count = 1; //累加器初始化
while(p != NULL && count < i) {
p = p->next;
count++;
}
if(p == NULL)
printf("Error Location!\n");
else {
*ptr = p->data; //通过指针返回值
return 1;
}
}
//插入元素操作
int Insert(Node * first, int i, DataType x) {
Node * s = NULL, *p = first; //工作指针初始化,指向头结点
int count = 0;
while(p != NULL && count < i) { //查找第i-1个节点
p = p->next;
count++;
}
if(p == NULL) {
printf("Error Location, Can't Insert!\n");
return 0;
} else {
s = (Node *)malloc(sizeof(Node)); //创建一个新节点
s->data = x;
s->next = p->next; //将s节点插到p之后
p->next = s;
return 1;
}
}
//建立单链表-头插法
Node *CreateList_Head(DataType a[], int n) {
Node *s = NULL;
Node *first = (Node *)malloc(sizeof(Node));
first->next = NULL; //初始化头结点
for(int i = 0; i < n; i++) { //为每个数组元素创建一个节点
s = (Node *) malloc(sizeof(Node)); //申请内存
s->data = a[i];
s->next = first->next; //将节点s插入到头结点之后
first->next = s;
}
return first;
}
//建立单链表-尾插法
Node *CreateList_End(DataType a[], int n) {
Node *s = NULL, *r = NULL;
Node *first = (Node*)malloc(sizeof(Node)); //生成头结点
r = first; //尾指针初始化
for(int i = 0; i < n; i++) {
s = (Node *)malloc(sizeof(Node));
s->data = a[i];
r->next = s; //将节点s插入到尾结点之后
r = s;
}
r->next = NULL; //单链表建立完毕,将终端节点指针域置空
return first;
}
//删除某个节点
int Delete(Node *first, int i, DataType *ptr) {
Node *p = first, *q = NULL; //工作指针p指向头结点
int count = 0;
DataType x;
while(p != NULL && count < i - 1) { //查找第i-1个节点
p = p->next;
count++;
}
if(p == NULL || p ->next == NULL) { //p节点或者p的后置节点不存在
printf("Error Location, Can't Delete!\n");
} else { //找到i节点,并删除,通过指针ptr返回节点数据
q = p->next;
*ptr = q->data;
p->next = q->next;
free(q);
return 1;
}
}
//销毁单链表
void DestroyList(Node *first) {
Node *p = first;
while(first != NULL) { //依次释放每个节点内存空间
first = first->next;
free(p);
p = first;
}
}
//主函数
int main() {
int r[5] = {1, 2, 3, 4, 5}, i, x;
Node *first = NULL;
first = CreateList_Head(r, 5);
printf("Print Current List: \n"); //打印当前链表
PrintList(first);
Insert(first, 2, 8); //在第2个节点插入元素8
printf("After Inserting , Current List is : \n"); //打印链表
PrintList(first);
printf("Print the length of current Seqlist : %d \n", Length(first)); //打印链表长度
printf("Please Input the value you want to find: \n"); //按位置查询数据
scanf("%d", &x);
i = Locate(first, x);
if(i == 0)
printf("Didn't Found! \n");
else
printf("the value (%d) you want to find is located in : %d \n", x, i);
printf("Please input the location you want to delete: \n"); //按位置删除元素
scanf("%d", &i);
if(Delete(first, i, &x) == 1)
{
printf("delete L[%d] is %d, Now the elements is : \n ", i, x); //打印删除的元素
PrintList(first); //打印删除后的链表
}
else printf("delete error!\n");
DestroyList(first); //销毁链表,释放内存空间
system("pause");
return 0;
}
编译运行后结果图: