线性表
链表
单向链表
#include <stdlib.h>
#include <stdio.h>
typedef int E;
struct ListNode{
E element;
struct ListNode * next;
};
typedef struct ListNode * Node;
void initList(Node node){
node->next = NULL;
}
bool insertList(Node head, E element, int index) {
if(index < 1) return 0;
while (--index){
head = head->next;
if(head == NULL) return 0;
}
Node node = static_cast<Node>(malloc(sizeof(struct ListNode)));
if(node == NULL) return 0;
node->element = element;
node->next = head->next;
head->next = node;
return 1;
}
bool deleteList(Node head, int index){
if(index < 1) return 0;
while(--index) {
head = head->next;
if(head == NULL) return 0;
}
if(head->next == NULL) return 0;
Node tmp = head->next;
head->next = head->next->next;
free(tmp);
return 1;
}
E * getList(Node head, int index){
if(index < 1) return 0;
do{
head = head->next;
if(head == NULL) return 0;
} while (--index);
return &head->element;
}
//查找对应元素位置
int findList(Node head, E element){
head = head->next;
int i = 1;
while(head){
if(head->element == element) return i;
head = head->next;
i++;
}
return -1;
}
//求链表的长度
int sizeList(Node head){
int i = -1;
while(head){
head = head->next;
i++;
}
return i;
}
void printList(Node head){
while (head->next){
head = head->next;
printf("%d ",head->element);
}
printf("\n");
}
int main(){
struct ListNode head;
initList(&head);
printf("%d\n",sizeList(&head));
for(int i = 1; i <= 3; i++){
insertList(&head,i*100,i);
}
deleteList(&head,4);
printList(&head);
printf("%d\n",*getList(&head,1));
printf("%d\n",findList(&head,100));
printf("%d\n",sizeList(&head));
}
双向链表
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct ListNode {
E element;
struct ListNode * next;//指向下一节点的指针
struct ListNode * prev;//指向上一节点的指针
};
typedef struct ListNode * Node;
//初始化
void initList(Node head){
head->next = head->prev = NULL;
}
//插入
_Bool insertList(Node head, E element, int index){
if(index < 1) return 0;
while (--index){
head = head->next;
if(head == NULL) return 0;
}
Node node = malloc(sizeof(struct ListNode));
if(node == NULL) return 0;
node->element = element;
if(head->next != NULL) {
node->next = head->next;
head->next->prev = node;
}else{
node->next = NULL;
}
head->next = node;
node->prev = head;
return 1;
}
//删除
_Bool deleteList(Node head, int index){
if(index < 1) return 0;
while (--index){
head = head->next;
if(head == NULL) return 0;
}
if(head->next == NULL) return 0;
Node tmp = head->next;
if(head->next->next){
head->next = head->next->next;
head->next->next->prev = head;
} else {
head->next = NULL;
}
free(tmp);
return 1;
}
int main(){
struct ListNode head;
initList(&head);
for (int i = 1; i <= 5; ++i)
insertList(&head,i * 100,i);
deleteList(&head,3);
Node node = &head; //正向遍历
do {
node = node->next;
printf("%d -> ",node->element);
}while (node->next != NULL);
printf("\n");
/*
do {//反向遍历
printf("%d -> ", node->element);
node = node->prev;
}while(node->prev != NULL);
*/
}
栈
用数组实现栈
#include <stdio.h>
#include <stdlib.h>
typedef int E ;
struct Stack{
E * array;
int capacity;//容量
int top;//栈顶元素下标
};
typedef struct Stack * ArrayStack;
//初始化
_Bool initStack(ArrayStack stack){
stack->array = malloc(sizeof(E) * 10);
if(stack->array == NULL) return 0;
stack->capacity = 10;
stack->top = -1;
return 1;
}
//遍历
void printStack(ArrayStack stack){
printf("| ");
for (int i = 0; i < stack->top + 1; ++i){
printf("%d, ",stack->array[i]);
}
printf("\n");
}
//入栈
_Bool pushStack(ArrayStack stack, E element){
if(stack->top + 1 == stack->capacity){//判断塞满没 塞满扩容
int newCapacity = stack->capacity + (stack->capacity >> 1);
E * newArray = realloc(stack->array, newCapacity * sizeof(E));
if(newArray == NULL) return 0;
stack->array = newArray;
stack->capacity = newCapacity;
}
stack->array[++stack->top] = element;
return 1;
}
//判断栈是否为空
_Bool isEmpty(ArrayStack stack){
return stack->top == -1;
}
//出栈
E popStack(ArrayStack stack){
return stack->array[stack->top--];
}
int main(){
struct Stack stack;
initStack(&stack);
for (int i = 0; i < 10; ++i){
pushStack(&stack, i * 100);
}
printStack(&stack);
while(!isEmpty(&stack)) {
printf("%d ", popStack(&stack));
}
}
用链表实现栈
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct LNode{
E element;
struct LNode * next;
};
typedef struct LNode * Node;
//初始化
void initStack(Node head){
head->next = NULL;
}
//入栈
_Bool pushStack(Node head, E element) {
Node node = malloc(sizeof(struct LNode));
if(node == NULL) return 0;
node->element = element;
node->next = head->next;
head->next = node;
return 1;
}
//遍历打印
void printStack(Node head){
//printf("| ");
head = head->next;
while(head){
printf("%d ",head->element);
head = head->next;
}
printf("|");
}
//判断是否为空
_Bool isEmpty(Node head){
return head->next == NULL;
}
//出栈
E popStack(Node head){
Node tmp = head->next;
E e = tmp->element;
head->next = head->next->next;
free(tmp);
return e;
}
int main(){
struct LNode head;
initStack(&head);
for (int i = 0; i < 3; ++i){
pushStack(&head,i * 100);
}
printStack(&head);
printf("\n");
while (!isEmpty(&head)){
printf("%d ", popStack(&head));
}
}
队列
顺序表实现队列
循环队列重复使用一个数组(大小固定 不能扩容)
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct Queue {
E * array;
int capacity;
int front, rear;
};
typedef struct Queue * ArrayQueue;
//初始化
_Bool initQueue(ArrayQueue queue){
queue->array = malloc(sizeof(E) * 10);
if(queue->array == NULL) return 0;
queue->capacity = 10;
queue->front = queue->rear = 0;
return 1;
}
//入队
_Bool offerQueue(ArrayQueue queue, E element){
int pos = (queue->rear + 1) % queue->capacity;
if(pos == queue->front)
return 0;
queue->rear = pos;
queue->array[queue->rear] = element;
return 1;
}
//判断是否为空
_Bool isEmpty(ArrayQueue queue){
return queue->rear == queue->front;
}
//出队
E pollQueue(ArrayQueue queue){
queue->front = (queue->front + 1) % queue->capacity;
return queue->array[queue->front];
}
//打印
void printQueue(ArrayQueue queue){
printf("<<< ");
int i = queue->front;
do {
i = (i + 1) % queue->capacity;
printf("%d ", queue->array[i]);
} while(i != queue->rear);
printf("<<<\n");
}
int main(){
struct Queue queue;
initQueue(&queue);
for (int i = 0; i < 5; i++) {
offerQueue(&queue, i * 100);
}
printQueue(&queue);
while (!isEmpty(&queue)) {
printf("%d ", pollQueue(&queue));
}
return 0;
}
用链表实现队列
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct LNode {
E element;
struct LNode * next;
};
typedef struct LNode * Node;
struct Queue {
Node front, rear;
};
typedef struct Queue * LinkedQueue;
//初始化
_Bool initQueue(LinkedQueue queue){
Node node = malloc(sizeof(struct LNode));
if(node == NULL) return 0;
queue->rear = queue->front = node;
return 1;
}
//入队
_Bool offerQueue(LinkedQueue queue, E element){
Node node = malloc(sizeof(struct LNode));
if(node == NULL) return 0;
node->element = element;
queue->rear->next = node;
queue->rear = node;
return 1;
}
//判断
_Bool isEmpty(LinkedQueue queue){
return queue->rear == queue->front;
}
//出队
E pollQueue(LinkedQueue queue){
Node tmp = queue->front->next;
E e = tmp->element;
queue->front->next = queue->front->next->next;
if(queue->rear == tmp) queue->rear = queue->front;
free(tmp);
return e;
}
//打印
void printQueue(LinkedQueue queue){
printf("<<< ");
Node node = queue->front->next;
do {
printf("%d ", node->element);
if(node == queue->rear) break; //当已经打印最后一个元素后,再结束
else node = node->next;
} while (node);
printf("<<<\n");
}
int main(){
struct Queue queue;
initQueue(&queue);
for (int i = 0; i < 5; ++i){
offerQueue(&queue, i*100);
}
printQueue(&queue);
while (!isEmpty(&queue)) {
printf("%d ", pollQueue(&queue));
}
}