线性表
线性表的顺序存储(顺序表)
#include <stdio.h>
#include <malloc.h>
const int MAXSIZE = 50;
typedef struct SequenceList
{
int* data;
int length;
}SqList;
void Initialize(SqList*, int); //初始化顺序表
bool AppendElem(SqList*, int); //添加元素
bool InsertElem(SqList*, int, int); //插入元素
bool DeleteElem(SqList*, int, int*); //删除元素
void Destroy(SqList*); //销毁顺序表
bool IsEmpty(SqList*); //是否为空
bool IsFull(SqList*); //是否已满
int GetLength(SqList*); //获取长度
int GetElem(SqList*, int); //按位查找
int LocateElem(SqList*, int); //按值查找
void Traverse(SqList*); //遍历顺序表
int main(void)
{
SqList L;
return 0;
}
void Initialize(SqList* L)
{
L->data = (int*)malloc(sizeof(int) * MAXSIZE);
L->length = 0;
}
bool AppendElem(SqList* L, int val)
{
if (IsFull(L))
{
return false;
}
else
{
L->data[L->length] = val;
L->length++;
return true;
}
}
bool InsertElem(SqList* L, int pos, int val)
{
if (IsFull(L))
{
return false;
}
else
{
for (int i = L->length; i >= pos; i--)
{
L->data[i] = L->data[i - 1];
}
L->data[pos - 1] = val;
L->length++;
return true;
}
}
bool DeleteElem(SqList* L, int pos, int* backVal)
{
if (IsEmpty(L))
{
return false;
}
else
{
*backVal = L->data[pos - 1];
for (int i = pos; i < L->length; i++)
{
L->data[i - 1] = L->data[i];
}
L->length--;
return true;
}
}
void Destroy(SqList* L)
{
free(L->data);
L->data = NULL;
}
bool IsEmpty(SqList* L)
{
if (L->length <= 0)
{
return true;
}
else
{
return false;
}
}
bool IsFull(SqList* L)
{
if (L->length >= MAXSIZE)
{
return true;
}
else
{
return false;
}
}
int GetLength(SqList* L)
{
return L->length;
}
int GetElem(SqList* L, int pos)
{
return L->data[pos - 1];
}
int LocateElem(SqList* L, int val)
{
for (int i = 0; i < L->length; i++)
{
if (L->data[i] == val)
{
return i + 1;
}
}
return -1;
}
void Traverse(SqList* L)
{
for (int i = 0; i < L->length; i++)
{
printf("%d ", L->data[i]);
}
printf("\n");
}
线性表的链式存储(单链表)
#include <stdio.h>
#include <malloc.h>
typedef struct ElemType
{
char name[8];
int score;
}ElemType;
typedef struct LinkNode
{
ElemType data;
struct LinkNode* pNext;
}LinkNode, * LinkList;
void HeadInitialize(LinkList*); //采用头插法初始化单链表
void TailInitialize(LinkList*); //采用尾插法初始化单链表
void AppendElem(LinkList, ElemType); //添加结点
bool Insert(LinkList, int, ElemType); //插入新结点
bool Delete(LinkList, int, ElemType*); //删除结点
void Destroy(LinkList); //销毁链表
bool IsEmpty(LinkList); //是否为空
int GetLength(LinkList); //计算表长
LinkNode* GetElem(LinkList, int); //按位查找结点
LinkNode* LocateElem(LinkList, ElemType); //按值查找结点
void Traverse(LinkList); //遍历单链表
int main(void)
{
LinkList L;
TailInitialize(&L);
Traverse(L);
AppendElem(L, { "sprout",100 });
Traverse(L);
Insert(L, 2, { "Insert",100 });
printf("在第2个结点处插入一个新节点\n");
Traverse(L);
ElemType e;
Delete(L, 4, &e);
printf("删除第4个结点,结点的名字是:%s,结点的成绩是:%d\n", e.name, e.score);
Traverse(L);
Destroy(L);
printf("销毁链表");
return 0;
}
void HeadInitialize(LinkList* pL)
{
int length;
*pL = (LinkNode*)malloc(sizeof(LinkNode));
(*pL)->pNext = NULL;
printf("请输入你想要创建的结点个数:");
scanf("%d", &length);
for (int i = 0; i < length; i++)
{
LinkNode* pNew = (LinkNode*)malloc(sizeof(LinkNode));
printf("请输入第%d个结点的姓名:", i + 1);
scanf("%s", &pNew->data.name);
printf("请输入第%d个结点的成绩:", i + 1);
scanf("%d", &pNew->data.score);
pNew->pNext = (*pL)->pNext;
(*pL)->pNext = pNew;
}
printf("\n");
}
void TailInitialize(LinkList* pL)
{
int length; //存储链表结点个数
*pL = (LinkNode*)malloc(sizeof(LinkNode));
LinkNode* pTail = *pL;
pTail->pNext = NULL;
printf("请输入您要创建的链表节点个数:");
scanf("%d", &length);
for (int i = 0; i < length; i++)
{
LinkNode* pNew = (LinkNode*)malloc(sizeof(LinkNode));
printf("请输入第%d个结点的姓名:", i + 1);
scanf("%s", &pNew->data.name);
printf("请输入第%d个结点的成绩:", i + 1);
scanf("%d", &pNew->data.score);
pNew->pNext = NULL;
pTail->pNext = pNew;
pTail = pNew;
}
printf("\n");
}
void AppendElem(LinkList L, ElemType e)
{
LinkNode* p = L;
while (p->pNext != NULL)
{
p = p->pNext;
}
int length = GetLength(L);
printf("在链表的第%d个位置新添一个结点\n", length + 1);
LinkNode* q = (LinkNode*)malloc(sizeof(LinkNode));
q->data = e;
q->pNext = NULL;
p->pNext = q;
}
bool Insert(LinkList L, int pos, ElemType e)
{
LinkNode* p = GetElem(L, pos - 1);
LinkNode* q = (LinkNode*)malloc(sizeof(LinkNode));
q->data = e;
q->pNext = p->pNext;
p->pNext = q;
return true;
}
bool Delete(LinkList L, int pos, ElemType* backElem)
{
if (IsEmpty(L))
{
return false;
}
LinkNode* p = GetElem(L, pos - 1);
LinkNode* q = p->pNext;
*backElem = q->data;
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
void Destroy(LinkList L)
{
LinkNode* p = L;
while (L != NULL)
{
L = L->pNext;
free(p);
p = L;
}
}
bool IsEmpty(LinkList L)
{
if (L->pNext == NULL)
{
return true;
}
return false;
}
int GetLength(LinkList L)
{
if (IsEmpty(L))
{
return 0;
}
int i = 1;
LinkNode* p = L->pNext;
while (p->pNext != NULL)
{
p = p->pNext;
i++;
}
return i;
}
LinkNode* GetElem(LinkList L, int pos)
{
if (pos < 1)
{
return NULL;
}
if (pos == 0)
{
return L;
}
int i = 1;
LinkNode* p = L->pNext;
while (i < pos && p != NULL)
{
p = p->pNext;
i++;
}
return p;
}
LinkNode* LocateElem(LinkList L, ElemType e)
{
for (LinkNode* p = L->pNext; p != NULL; p = p->pNext)
{
if (p->data.name == e.name && p->data.score == e.score)
{
return p;
}
}
return NULL;
}
void Traverse(LinkList L)
{
int i = 1;
for (LinkNode* p = L->pNext; p != NULL; p = p->pNext)
{
printf("链表第%d个结点的名字是:%s,成绩是:%d\n", i, p->data.name, p->data.score);
i++;
}
}
栈
栈的顺序存储(顺序栈)
#include <stdio.h>
#include <malloc.h>
const int MaxSize = 50;
typedef struct SequenceStack
{
int* data;
int top;
}SqStack;
void Initialize(SqStack*);
bool Push(SqStack*, int);
bool Pop(SqStack*, int*);
void Destroy(SqStack*);
bool IsEmpty(SqStack*);
bool IsFull(SqStack*);
int GetTop(SqStack*);
int main(void)
{
SqStack s;
return 0;
}
void Initialize(SqStack* pS)
{
pS->data = (int*)malloc(sizeof(int) * MaxSize);
pS->top = 0;
}
bool Push(SqStack* pS, int val)
{
if (pS->top == MaxSize)
{
return false;
}
else
{
pS->data[pS->top] = val;
pS->top++;
return true;
}
}
bool Pop(SqStack* pS, int* backVal)
{
if (pS->top == 0)
{
return false;
}
else
{
pS->top--;
*backVal = pS->data[pS->top];
return true;
}
}
void Destroy(SqStack* pS)
{
free(pS->data);
pS->top = 0;
}
bool IsEmpty(SqStack* pS)
{
if (pS->top == 0)
{
return true;
}
return false;
}
bool IsFull(SqStack* pS)
{
if (pS->top == MaxSize)
{
return true;
}
return false;
}
int GetTop(SqStack* pS)
{
return pS->data[pS->top - 1];
}
栈的链式存储(链栈)
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode
{
int data;
struct LinkNode* next;
}LNode;
typedef struct LinkStack
{
LNode* top;
LNode* bottom;
}LStack;
void Initialize(LStack*);
void Push(LStack*, int);
bool Pop(LStack*, int*);
void Destroy(LStack*);
bool IsEmpty(LStack*);
int GetTop(LStack* pS);
int main(void)
{
LStack s;
return 0;
}
void Initialize(LStack* pS)
{
pS->top = (LNode*)malloc(sizeof(LNode));
pS->bottom = pS->top;
pS->top->next = NULL;
}
void Push(LStack* pS, int val)
{
LNode* pNew = (LNode*)malloc(sizeof(LNode));
pNew->data = val;
pNew->next = pS->top;
pS->top = pNew;
}
bool Pop(LStack* pS, int* backVal)
{
if (pS->top == pS->bottom)
{
return false;
}
LNode* p = pS->top->next;
*backVal = pS->top->data;
free(pS->top);
pS->top = p;
return true;
}
void Destroy(LStack* pS)
{
LNode* p = pS->top->next;
while (pS->top != pS->bottom)
{
free(pS->top);
pS->top = p;
p = p->next;
}
}
bool IsEmpty(LStack* pS)
{
if (pS->top == pS->bottom)
{
return true;
}
return false;
}
int GetTop(LStack* pS)
{
return pS->top->data;
}
队列
队列的顺序存储(顺序队)
#include <stdio.h>
#include <malloc.h>
const int MaxSize = 50;
typedef struct SequenceQueue
{
int* data;
int front;
int rear;
}SqQueue;
void Initialize(SqQueue*);
bool EnQueue(SqQueue*, int);
bool DeQueue(SqQueue*, int*);
bool IsEmpty(SqQueue*);
bool IsFull(SqQueue*);
int GetFront(SqQueue*);
int main(void)
{
SqQueue q;
return 0;
}
void Initialize(SqQueue* pQ)
{
pQ->data = (int*)malloc(sizeof(int) * MaxSize);
pQ->front = pQ->rear = 0;
}
bool EnQueue(SqQueue* pQ, int val)
{
if ((pQ->rear + 1) % MaxSize == pQ->front) //rear的下一个位置是front,说明队列已满
{
return false;
}
else
{
pQ->data[pQ->rear] = val;
pQ->rear = (pQ->rear + 1) % MaxSize;
return true;
}
}
bool DeQueue(SqQueue* pQ, int* backVal)
{
if (pQ->rear == pQ->front) //rear和front的位置重合,说明队列为空
{
return false;
}
else
{
*backVal = pQ->data[pQ->front];
pQ->front = (pQ->front + 1) % MaxSize;
return true;
}
}
bool IsEmpty(SqQueue* pQ)
{
if (pQ->rear == pQ->front)
{
return true;
}
return false;
}
bool IsFull(SqQueue* pQ)
{
if ((pQ->rear + 1) % MaxSize == pQ->front)
{
return true;
}
return false;
}
int GetFront(SqQueue* pQ)
{
return pQ->data[pQ->front];
}
队列的链式存储(链队)
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode
{
int data;
struct LinkNode* next;
}LNode;
typedef struct LinkQueue
{
LNode* front;
LNode* rear;
}LQueue;
void Initialize(LQueue*);
void EnQueue(LQueue*, int);
bool DeQueue(LQueue*, int*);
int main(void)
{
LQueue q;
return 0;
}
void Initialize(LQueue* pQ)
{
pQ->rear = (LNode*)malloc(sizeof(LNode));
pQ->front = pQ->rear;
pQ->front->next = NULL;
}
void EnQueue(LQueue* pQ, int val)
{
LNode* pNew = (LNode*)malloc(sizeof(LNode));
pNew->data = val;
pNew->next = NULL;
pQ->rear->next = pNew;
pQ->rear = pNew;
}
bool DeQueue(LQueue* pQ, int* backVal)
{
if (pQ->rear == pQ->front)
{
return false;
}
else
{
*backVal = pQ->front->next->data;
LNode* p = pQ->front->next;
pQ->front->next = p->next;
if (p == pQ->rear) //如果队列中此时只有一个结点
{
pQ->rear = pQ->front;
}
free(p);
p = NULL;
return true;
}
}