数据结构基本操作

创建,先序,中序,后序,层次遍历。

#include<stdio.h>
#include<stdlib.h>

typedef struct TreeNode {
	char data;
	struct TreeNode* lchild;
	struct TreeNode* rchild;
}TreeNode;

typedef struct Node {
	struct TreeNode* tree_data;
	struct Node* next;
}Node;

void initTree(TreeNode** T, char* data, int* index) {

	char str = data[*index];
	(*index)++;  
	// ++的运行级别高于*,所以加括号先运算取出*index,然后++

	if (str == '#') {
		*T = NULL;
		return;
	}
	else {
		*T = (TreeNode*)malloc(sizeof(TreeNode));
		(*T)->data = str;  
		//*T->data 表示的是*(T->data),所以*T要加括号变为(*T),然后(*T)->data,这样才是*T指向的data
		initTree(&(*T)->lchild, data, index);
		initTree(&(*T)->rchild, data, index);
	}
}

void preOrder(TreeNode* T) {
	if (T == NULL) {
		return;
	}
	else {
		printf("%c ", T->data);
		preOrder(T->lchild);
		preOrder(T->rchild);
	}
}

void midOrder(TreeNode* T) {
	if (T == NULL) {
		return;
	}
	else {
		midOrder(T->lchild);
		printf("%c ", T->data);
		midOrder(T->rchild);
	}
}

void endOrder(TreeNode* T) {
	if (T == NULL) {
		return;
	}
	else {
		endOrder(T->lchild);
		endOrder(T->rchild);
		printf("%c ", T->data);
	}
}

Node* initQueue() {
	Node* Q = (Node*)malloc(sizeof(Node));
	TreeNode* data = (TreeNode*)malloc(sizeof(TreeNode));
	data->data = 0;
	data->lchild = NULL;
	data->rchild = NULL;
	Q->tree_data = data;
	Q->next = NULL;
	return Q;
}

void inQueue(Node* Q, TreeNode* data) {
	Node* temp = (Node*)malloc(sizeof(Node));
	temp->tree_data = data;
	temp->next = NULL;

	Node* curr = Q;
	while (curr->next)
	{
		curr = curr->next;
	}
	curr->next = temp;
	((Q->tree_data)->data)++;
}

TreeNode* outQueue(Node* Q) {
	Node* temp = Q->next;
	Q->next = temp->next;
	TreeNode* data = temp->tree_data;
	free(temp);
	((Q->tree_data)->data)--;
	return data;
}

void levelOrder(TreeNode* T) {
	Node* Q = initQueue();
	inQueue(Q, T);

	while (((Q->tree_data)->data) != 0)
	{
		TreeNode* temp = outQueue(Q);
		if (temp->lchild != NULL) {
			inQueue(Q, temp->lchild);
		}
		if (temp->rchild != NULL) {
			inQueue(Q, temp->rchild);
		}
		printf("%c ", temp->data);
	}
}

Node* initStack() {
	Node* S = (Node*)malloc(sizeof(Node));
	TreeNode* data = (TreeNode*)malloc(sizeof(TreeNode));
	data->data = 0;
	data->lchild = NULL;
	data->rchild = NULL;
	S->tree_data = data;
	S->next = NULL;
	return S;
}

void push(Node* S, TreeNode* data) {
	Node* temp = (Node*)malloc(sizeof(Node));
	temp->tree_data = data;
	temp->next = S->next;
	S->next = temp;
	((S->tree_data)->data)++;
}

TreeNode* pop(Node* S) {
	Node* temp = S->next;
	//printf("pop:%c\n", temp->data.data);

	S->next = temp->next;
	((S->tree_data)->data)--;
	TreeNode* data = temp->tree_data;
	free(temp);
	return data;
}

void pre_order(TreeNode* T) { //非递归先序遍历
	Node* s = initStack();
	while (T)
	{
		printf("%c ", (*T).data);
		push(s, T);
		T = T->lchild;
	}
	while (((s->tree_data)->data) != 0)
	{
		TreeNode* temp = pop(s);

		if (temp->rchild) {
			TreeNode* Temp = temp->rchild;
			while (Temp)
			{
				printf("%c ", (*Temp).data);
				push(s, Temp);
				Temp = Temp->lchild;
			}
		}
	}
}

void mid_order(TreeNode* T) { //非递归中序遍历
	Node* s = initStack();
	while (T)
	{
		push(s, T);
		T = T->lchild;
	}
	while (((s->tree_data)->data) != 0)
	{
		TreeNode* temp = pop(s);
		printf("%c ", temp->data);

		if (temp->rchild) {
			TreeNode* Temp = temp->rchild;
			while (Temp)
			{
				push(s, Temp);
				Temp = Temp->lchild;
			}
		}
	}
}

void end_order(TreeNode* T) { //非递归后序遍历
	Node* s = initStack();
	while (T)
	{
		push(s, T);
		T = T->lchild;
	}
	TreeNode* flag = NULL;
	while (((s->tree_data)->data) != 0)
	{
		TreeNode* temp = pop(s);

		if (temp->rchild == NULL || temp->rchild == flag) {  //右为空或已经访问
			printf("%c ", temp->data);
			flag = temp;
		}
		else
		{
			push(s, temp);
			TreeNode* Temp = temp->rchild;
			while (Temp)
			{
				push(s, Temp);
				Temp = Temp->lchild;
			}
		}
	}
}

int main() {
	TreeNode * T;

	int index = 0;

	char data[100];
	scanf_s("%s", data, 100);
	printf("%s\n", data);

	initTree(&T, data, &index);

	//递归 先序 中序 后序 遍历
	printf("pre ");
	preOrder(T);
	printf("\n");

	printf("mid ");
	midOrder(T);
	printf("\n");

	printf("end ");
	endOrder(T);
	printf("\n");

	//层次遍历
	printf("level ");
	levelOrder(T);
	printf("\n");

	//非递归 先序 中序 后序 遍历
	printf("pre_ ");
	pre_order(T);
	printf("\n");

	printf("min_ ");
	mid_order(T);
	printf("\n");

	printf("end_ ");
	end_order(T);
	printf("\n");

	system("pause");
	return 0;
}

循环队列

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 5

// 循环队列
typedef struct Queue {
	int pre;
	int rear;
	int data[MAXSIZE];
}Queue;

Queue* initQueue() {
	Queue* q = (Queue*)malloc(sizeof(Queue));
	q->pre = 0;
	q->rear = 0;
	return q;
}

int isFull(Queue* q) {
	if ((q->rear + 1) % MAXSIZE == q->pre) {
		return 1;
	}
	else {
		return 0;
	}
}

int isEmpty(Queue* q) {
	if (q->rear == q->pre) {
		return 1;
	}
	else {
		return 0;
	}
}

int inQueue(Queue* q, int data) {
	if (isFull(q) == 1) {
		printf("is full, %d in Queue error \n", data);
		return 0;
	}
	else {
		q->data[q->rear] = data;
		q->rear = (q->rear + 1) % MAXSIZE;
		return 1;
	}
}

int outQueue(Queue* q) {
	if (isEmpty(q) == 1) {
		printf("is emnpty \n");
		return 0;
	}
	else {
		int temp = q->data[q->pre];
		printf("out %d\n", temp);
		q->pre = (q->pre + 1) % MAXSIZE;
		return 1;
	}
}

void printQueue(Queue* q) {
	int len = (q->rear - q->pre + MAXSIZE) % MAXSIZE;
	printf("print queue ");
	for (int i = 0; i < len; i++) {
		int curr = (q->pre + i) % MAXSIZE;
		printf("%d", q->data[curr]);
	}
	printf("\n");
}

int main() {
	Queue* q = initQueue();

	outQueue(q);

	inQueue(q, 1);
	inQueue(q, 2);
	inQueue(q, 3);

	printQueue(q);

	outQueue(q);
	outQueue(q);

	printQueue(q);

	inQueue(q, 4);
	inQueue(q, 5);
	inQueue(q, 6);
	inQueue(q, 8);
	inQueue(q, 9);

	printQueue(q);

	system("pause");
	return 0;
}

队列

#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
	int data;
	struct Node* next;
}Node;

Node* initQueue() {
	Node* Q = (Node*)malloc(sizeof(Node));
	Q->data = 0;
	Q->next = NULL;
	return Q;
}

void inQueue(Node* Q, int data) {
	Node* temp = (Node*)malloc(sizeof(Node));
	temp->data = data;
	temp->next = NULL;

	Node* curr = Q;
	while (curr->next)
	{
		curr = curr->next;
	}
	curr->next = temp;
	Q->data++;
}

void outQueue(Node* Q) {
	Node* temp = Q->next;
	printf("out:%d\n", temp->data);

	Q->next = temp->next;
	free(temp);
	Q->data--;
}

void printQueue(Node* Q) {
	Node* temp = Q->next;
	printf("len:%d\n", Q->data);
	while (temp)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}
	printf("\n");
}

int main() {
	Node* Q = initQueue();

	inQueue(Q, 1);
	inQueue(Q, 2);
	inQueue(Q, 3);
	inQueue(Q, 4);
	inQueue(Q, 5);

	printQueue(Q);

	outQueue(Q);
	outQueue(Q);
	outQueue(Q);

	printQueue(Q);

	inQueue(Q, 1);
	inQueue(Q, 2);

	printQueue(Q);

	system("pause");
	return 0;
}

#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
	int data;
	struct Node* next;
}Node;

Node* initStack() {
	Node* S = (Node*)malloc(sizeof(Node));
	S->data = 0;
	S->next = NULL;
	return S;
}

void push(Node* S, int data) {
	Node* temp = (Node*)malloc(sizeof(Node));
	temp->data = data;
	temp->next = S->next;
	S->next = temp;
	S->data++;
}

void pop(Node* S) {
	Node* temp = S->next;
	printf("pop:%d\n", temp->data);

	S->next = temp->next;
	S->data--;
	free(temp);
}

void printStack(Node* S) {
	Node* temp = S->next;
	printf("len:%d\n", S->data);
	while (temp)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}
	printf("\n");
}

int main() {
	Node* S = initStack();
	push(S, 1);
	push(S, 2);
	push(S, 3);
	push(S, 4);
	push(S, 5);

	printStack(S);

	pop(S);
	pop(S);
	pop(S);

	printStack(S);

	push(S, 4);
	push(S, 5);

	printStack(S);

	system("pause");
	return 0;
}

循环双链表

#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
	int data;
	struct Node* pre;
	struct Node* next;
} Node;


Node* initLinkedList() {
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = head;
	head->pre = head;
	head->data = 0;
	return head;
}


void insertAtHead(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;

	if (head->data == 0) {
		newNode->next = head->next;
		newNode->pre = head;
		head->next = newNode;
		head->pre = newNode;
		head->data++;
	}
	else {
		newNode->next = head->next;
		newNode->pre = head;
		head->next->pre = newNode;
		head->next = newNode;
		head->data++;
	}
}


void insertAtTail(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;

	Node* curr = head;
	while (curr->next != head) {
		curr = curr->next;
	}

	newNode->next = curr->next;
	newNode->pre = curr;
	curr->next->pre = newNode;
	curr->next = newNode;

	head->data++;
}


void deleteNode(Node* head, int value) {
	Node* curr = head->next;

	while (curr != head) {
		if (curr->data == value) {

			curr->next->pre = curr->pre;
			curr->pre->next = curr->next;

			free(curr);
			head->data--;
			return;
		}
		curr = curr->next;
	}
}


void traverseLinkedList(Node* head) {
	Node* curr = head->next;
	printf("len:%d\n", head->data);
	while (curr != head) {
		printf("%d ", curr->data);
		curr = curr->next;
	}
	printf("\n");
}


int main() {
	Node* head = initLinkedList();

	insertAtHead(head, 1);
	insertAtHead(head, 2);
	insertAtHead(head, 3);

	traverseLinkedList(head);

	insertAtTail(head, 4);
	insertAtTail(head, 5);
	insertAtTail(head, 6);

	traverseLinkedList(head);

	deleteNode(head, 3);
	deleteNode(head, 6);

	traverseLinkedList(head);

	system("pause");

	return 0;
}

双链表

#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
	int data;
	struct Node* pre;
	struct Node* next;
} Node;


Node* initLinkedList() {
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = NULL;
	head->pre = NULL;
	head->data = 0;
	return head;
}


void insertAtHead(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;

	if (head->data == 0) {
		newNode->next = head->next;
		newNode->pre = head;
		head->next = newNode;
		head->data++;
	}
	else{
		newNode->next = head->next;
		newNode->pre = head;
		head->next->pre = newNode;
		head->next = newNode;
		head->data++;
	}
}


void insertAtTail(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;
	newNode->next = NULL;

	Node* curr = head;
	while (curr->next != NULL) {
		curr = curr->next;
	}
	curr->next = newNode;
	newNode->pre = curr;
	head->data++;
}


void deleteNode(Node* head, int value) {
	Node* curr = head->next;

	while (curr != NULL) {
		if (curr->data == value) {
			if (curr->next == NULL) {
				curr->pre->next = NULL;

				free(curr);
				head->data--;
				return;
			}
			else {
				curr->next->pre = curr->pre;
				curr->pre->next = curr->next;

				free(curr);
				head->data--;
				return;
			}
		}
		curr = curr->next;
	}
}


void traverseLinkedList(Node* head) {
	Node* curr = head->next;
	printf("len:%d\n", head->data);
	while (curr != NULL) {
		printf("%d ", curr->data);
		curr = curr->next;
	}
	printf("\n");
}


int main() {
	Node* head = initLinkedList();

	insertAtHead(head, 1);
	insertAtHead(head, 2);
	insertAtHead(head, 3);

	traverseLinkedList(head);

	insertAtTail(head, 4);
	insertAtTail(head, 5);
	insertAtTail(head, 6);

	traverseLinkedList(head);

	deleteNode(head, 3);
	deleteNode(head, 6);

	traverseLinkedList(head);

	system("pause");

	return 0;
}

循环单链表

#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
	int data;
	struct Node* next;
} Node;


Node* initLinkedList() {
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = head;
	return head;
}


void insertAtHead(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;

	newNode->next = head->next;
	head->next = newNode;
}


void insertAtTail(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;
	newNode->next = head;

	Node* curr = head;
	while (curr->next != head) {
		curr = curr->next;
	}
	curr->next = newNode;
}


void deleteNode(Node* head, int value) {
	Node* prev = head;
	Node* curr = head->next;

	while (curr != head) {
		if (curr->data == value) {
			prev->next = curr->next;
			free(curr);
			return;
		}
		prev = curr;
		curr = curr->next;
	}
}


void traverseLinkedList(Node* head) {
	Node* curr = head->next;
	while (curr != head) {
		printf("%d ", curr->data);
		curr = curr->next;
	}
	printf("\n");
}


int main() {
	Node* head = initLinkedList();

	insertAtHead(head, 1);
	insertAtHead(head, 2);
	insertAtHead(head, 3);

	traverseLinkedList(head);

	insertAtTail(head, 4);
	insertAtTail(head, 5);
	insertAtTail(head, 6);

	traverseLinkedList(head);

	deleteNode(head, 2);
	deleteNode(head, 5);

	traverseLinkedList(head);

	system("pause");

	return 0;
}

单链表

#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
	int data;          
	struct Node* next;
} Node;


Node* initLinkedList() {
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = NULL; 
	return head;
}


void insertAtHead(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value; 

	newNode->next = head->next;
	head->next = newNode; 
}


void insertAtTail(Node* head, int value) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = value;   
	newNode->next = NULL;

	Node* curr = head;
	while (curr->next != NULL) {
		curr = curr->next;
	}
	curr->next = newNode;
}


void deleteNode(Node* head, int value) {
	Node* prev = head;
	Node* curr = head->next;

	while (curr != NULL) {
		if (curr->data == value) {   
			prev->next = curr->next;    
			free(curr);     
			return;
		}
		prev = curr;
		curr = curr->next;
	}
}


void traverseLinkedList(Node* head) {
	Node* curr = head->next;
	while (curr != NULL) {
		printf("%d ", curr->data);
		curr = curr->next;
	}
	printf("\n");
}


int main() {
	Node* head = initLinkedList();

	insertAtHead(head, 1);
	insertAtHead(head, 2);
	insertAtHead(head, 3);

	traverseLinkedList(head);

	insertAtTail(head, 4);
	insertAtTail(head, 5);
	insertAtTail(head, 6);

	traverseLinkedList(head);

	deleteNode(head, 2);
	deleteNode(head, 5);

	traverseLinkedList(head);

	system("pause");

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值