数据结构顺序表链表常见操作
#include<stdio.h>
#include<string.h>
#define true 1
#define error 0
#define MAX 100
#define ElemType int
//顺序表
typedef struct
{
int data[MAX];//顺序表元素
int length;//顺序表当前长度
}Sqlist;
//数据元素的数据项
typedef struct
{
int id;
char *name;
};
//初始化顺序表
void initList(Sqlist *L)
{
if (length > MAX)
{
return error;
}
memset(L->data, 0, sizeof(L));//初始化数据为0
L->length = 0;
}
//创建顺序表
void create(Sqlist* L, int n)//初始化n个数据
{
if (n<0 || n>MAX)
{
return error;
}
else
{
for (int i = 0; i < n; i++)
{
scanf("%d",&L->data[i]);
L->length++;
}
}
}
//插入元素
void insertList(Sqlist* L, int i, ElemType e)
{
if (i<1 || i>L->length + 1||L->length>MAX)
{
return error;
}
for (int j = L->length; j >= i; j--)//后移
{
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = e;
L->length++;
return true;
}
//----------------------------------------
//单链表
typedef struct Node
{
int data;
struct node *next
}node,*LinkList;
//遍历单链表
void displaynode(LinkList L)
{
LinkList p = (LinkList)malloc(sizeof(node));
p = L->next;
while (p != NULL)
{
printf("%d", p->data);
p = p->next;
}
}
//求链表中元素个数
int length(LinkList L)
{
int count = 0;
LinkList p = (LinkList)malloc(sizeof(node));
p = L->next;
while (p != NULL)
{
p = p->next;
count++;
}
return 0;
}
//查找元素
int searchnode(LinkList L, int x)//查找x
{
LinkList p = (LinkList)malloc(sizeof(node));
p = L->next;
while (p != NULL)
{
if (p->data == x)
{
printf("%d", p->data);
return true;
}
p = p->next;
}
return error;
}
//插入元素 假设在有一个p结点 在后面插入node结点
int insertnode(LinkList L, int i, int x)//在i位置插入 x
{
LinkList p = (LinkList)malloc(sizeof(node));
p = L;//工作指针p指向头结点
int count = 0;
while (p && count < i - 1)
//查找第i-1个位置,-1就是找到那个位置,
//插到他的前面,也就是插到了那个位置
{
p = p->next;
count++;
}
if (p == NULL)//到最后一个结点都没有找到要插入的位置
{
return error;
}
else
{
LinkList node = (LinkList)malloc(sizeof(node));
node->data = x;//把插入结点的数据域赋为要插入的值
node->next = p->next;
p->next = node;
return true;
}
}
//创建单链表
int a[] = { 35,12,24,33,42 };//创建一个数组
//头插法
LinknewList(int a[], int n)//把数组a添加进去,共n个元素
{
//创建头结点
LinkList L = (LinkList)malloc(sizeof(node));
L->next = NULL;
//创建后续结点
for (int i = 0; i < n; i++)
{
LinkList node = (LinkList)malloc(sizeof(node));
node->data = a[i];
node->next = L->next;
L->next = node;
}
return L;
}
//尾插法
LinknewList(int a[], int n)
{
LinkList L = (LinkList)malloc(sizeof(node));
L->next = NULL;
LinkList rear = L;//刚开始头结点和尾结点是同一个 初始化尾指针
for (int i = 0; i < n; i++)
{
LinkList node = (LinkList)malloc(sizeof(node));
node->next = NULL;//创建一个结点指针域就赋值为空
node->data = a[i];
rear->next = node;
rear = node;
}
rear->next = NULL;
return L;
}
//删除结点
int deletenode(LinkList L, int x)//删除x元素
{
if (L == NULL || L->next == NULL)
{
return error;
}
LinkList p = L->next;
LinkList q = L;
while (p)
{
if (p->data == x)
{
q->next = p->next;
free(p);
return true;
}
else
{
q = p;
p = p->next;
}
}
return error;//到循环结束都没有找到x元素
}
//释放单链表
int clearnode(LinkList L)
{
LinkList L = (LinkList)malloc(sizeof(node));
while (L)
{
LinkList q = L;
L = L->next;
free(q);
}
}
//循环列表就是最后一个结点指向不同
//栈和队列
//顺序栈
#define StackElemType int
typedef struct
{
StackElemType elem[MAX];
int top;
}SeqStack;
//初始化
void initStack(SeqStack* s)
{
s->top = -1;
}
//栈空?
int isempty(SeqStack* s)
{
return(s->top == -1 ? true : error);
}
//栈满?
int isfull(SeqStack* s)
{
return(s->top==MAX?true:error);
}
//进栈
int push(SeqStack* s, StackElemType x)//x进栈
{
if (s->top == MAX)
{
return error;
}
s->top++;
s->elem[s->top] = x;
return true;
}
//出栈
int pop(SeqStack* s, StackElemType* x)
{
if (s->top == -1)
{
return error;
}
else
{
*x = s->elem[s->top];
s->top--;
return true;
}
}
//取栈顶元素
int gettop(SeqStack* s, StackElemType* x)
{
if (s->top == -1)
{
return error;
}
else
{
*x = s->elem[s->top];
}
}
//两栈
typedef struct
{
StackElemType Stack[MAX];
StackElemType top[2];//top[0/1]是两个栈顶指示器
}DqStack;
//两栈进栈
int push(DqStack* s, StackElemType x, int i)
{
if (s->top[0] + 1 == s->top[1])
{
return error;
}
switch (i)
{
case 0:s->top[0]++; s->Stack[s->top[0]] = x; break;
case 1:s->top[1]++; s->Stack[s->top[1]] = x; break;
default:return error;
}
return true;
}
//两栈出栈
int pop(DqStack* s, StackElemType *x, int i)
{
switch (i)
{
case 0:if (s->top[0] == -1) return error;
*x = s->Stack[s->top[0]]; s->top[0]--; return true;
case 1:if (s->top[1] == MAX) return error;
*x = s->Stack[s->top[1]]; s->top[1]--; return true;
}
return true;
}
//链栈
typedef struct node
{
StackElemType data;
struct node* next;
}LinkStackNode,*LinkStack;
//进栈
int push(LinkStack top, StackElemType x)
{
LinkStack t = (LinkStack)malloc(sizeof(LinkStackNode));
if (t == NULL)
{
return error;
}
else
{
t->data = x;
t->next = top->next;
top->next = t;
}
}
//出栈
int pop(LinkStack top, StackElemType* x)
{
LinkStack t = top->next;
if (t == NULL)
{
return error;
}
top->next = t->next;
*x = t->data;
free(t);
return true;
}
#define QueueElemType int
//队列
typedef struct node
{
QueueElemType data;//数据域
struct node *next//指针域
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;//前指针
LinkQueueNode *rear;//后指针
}LinkQueue;
//初始化队列
int initQueue(LinkQueue* Q)
{
Q->front = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if (Q->front != NULL)
{
Q->rear = Q->front;
Q->front->next = NULL;
return true;
}
else//申请空间失败
{
return error;
}
}
//入队操作
int enterQueue(LinkQueue* Q, QueueElemType x)
{
LinkQueueNode* newnode = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if (newnode != NULL)
{
newnode->data = x;
newnode->next = NULL;
Q->rear->next = newnode;
Q->rear = newnode;
return true;
}
else
{
return error;
}
}
//出队
int deleteQueue(LinkQueue* Q, QueueElemType *x)
{
LinkQueueNode* p;
if (Q->front == Q->rear)
{
return error;
}
else
{
p = Q->front->next;
Q->front->next = p->next;//p出队
}
if (Q->rear == p)//队头只有一个元素p,出队后为空队
{
Q->rear = Q->front;
*x = p->data;
free(p);
return true;
}
}
//顺
序队列
typedef struct
{
QueueElemType elem[MAX];
int front;
int rear;
}SeqQueue;
//初始化循环队列
void initQueue(SeqQueue* Q)
{
Q->front = Q->rear = 0;
}
//循环队列入队
int EnterQueue(SeqQueue* Q, QueueElemType x)//元素x入队
{
if ((Q->rear + 1) % MAX == Q->front)//队列已满
{
return error;
}
else
{
Q->elem[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAX;//重设置队尾指针
return true;
}
}
//循环队列出队
int DeleteQueue(SeqQueue* Q, QueueElemType* x)
{
if (Q->front == Q->rear)
{
return error;
}
else
{
*x = Q->elem[Q->front];
Q->front = (Q->front + 1) % MAX;//重新设置队头指针
return true;
}
作者:吕文康
学校:山东第一医科大学
2020年12月4日