数据结构
2016-05-01 23:49
447 查看
数据结构
链表
链表是一种存储结构,指的是存储时候除了要存储数据元素外,还要数据元素一起的另外空间存储数据元素的关系。单向链表
#include<stdio.h> #include<stdlib.h> #include<time.h> typedef struct linklist { int num; linklist *next; }linknode, *linklistp; //在链表的头部插入节点 linklistp insert_head(linklistp head,linklistp newnode) { if (newnode!=nullptr) { newnode->next = head; head = newnode; } return head; } //在链表的尾部插入节点 linklistp insert_tail(linklistp head, linklistp newnode) { if (newnode==nullptr) return head; //当链表表头为没有元素时 if (head==nullptr) { head = newnode; } else { linklistp temp = head; while (temp->next!=nullptr) temp = temp->next; temp->next = newnode; newnode->next = nullptr; } return head; } linklistp insert_node(linklistp head, int a, int data)//在数据data前插入a { return head; }//通过链表数据删除链表节点 linklistp delete_node(linklistp head, int data) { if (head==nullptr) { return nullptr; } else if (head->num == data) { head = head->next; return head; } linklistp temp = head; linklistp pre = head; while (temp!=nullptr && temp->num != data) { pre = temp;//pre记录前一个节点 temp = temp->next; } if (temp == nullptr) return nullptr; else pre->next = temp->next; free(temp);//释放temp所指对象的内存空间 return head; } //替换节点 linklistp replace_node(linklistp head,int data,int new_data)//在//用新节点替换data,新节点数据为new_data { linklistp temp = head; if (temp->num == data) { linklistp new_node = (linklistp)malloc(sizeof(linklist)); new_node->num = new_data; new_node->next = temp->next; free(temp); return new_node; } linklistp pre=temp; while (temp!= nullptr&&temp->num != data) { pre = temp; temp = temp->next; } if (temp == nullptr) { return nullptr; } else { linklistp new_node = (linklistp)malloc(sizeof(linklist)); new_node->num = new_data; new_node->next = temp->next; pre->next = new_node; free(temp); } return head; } void output(linklistp head) { linklistp temp = head; while (temp) { printf("%d ", temp->num); temp = temp->next; } printf("\n"); } linklistp cheak_node(linklistp head, int data) { linklistp temp = head; while (temp != nullptr&&temp->num != data) { temp = temp->next; } if (temp == nullptr) return nullptr; return temp; } linklistp creat_list() { linklistp head = nullptr; srand(time(nullptr)); for (int i = 1; i <6; i++) { linklistp newnode = (linklistp)malloc(sizeof(linknode)); newnode->num = rand() % 100; newnode->next = nullptr;//将节点的next赋值无空 head = insert_tail(head, newnode);//插后面 output(head); getchar(); } for (int i = 1; i < 6; i++) { linklistp newnode = (linklistp)malloc(sizeof(linknode)); newnode->num = rand() % 100; newnode->next = nullptr;//将节点的next赋值无空 head = insert_head(head, newnode);//插前面 output(head); getchar(); } return head; } int main() { linklistp head = creat_list(); int data = 0; printf("please input num for delete"); scanf_s("%d",&data); linklistp temp = delete_node(head, data); if (temp==nullptr) { printf("Can't seach data"); } else { head = temp; output(head); } printf("please input num for replace"); scanf_s("%d", &data); printf("please input replace data"); int new_data = 0; scanf_s("%d", &new_data); temp = replace_node(head, data,new_data); if (temp == nullptr) { printf("Can't seach data for replace"); } else { head = temp; output(head); } printf("please input data fo cheak"); scanf_s("%d", &data); temp=cheak_node(head, data); if (temp == nullptr) { printf("Can't find data"); } else { printf("found data"); } return 0; }
单向循环链表
#include<stdio.h> #include<stdlib.h> #include<time.h> typedef struct linklist { int num; linklist *next; }linknode, *linklistp; void output(linklistp head) { linklistp temp = head; while(temp->next != head) { printf("%d ", temp->num); temp = temp->next; } if (temp->next == head) { printf("%d ", temp ->num); } printf("\n"); } linklistp insert_node(linklistp head,linklistp new_node) { if (new_node == nullptr) return head; //当链表表头为没有元素时 if (head == nullptr) { head = new_node; head->next = new_node; } else { linklistp temp = head; while (temp->next != head) temp = temp->next; temp->next = new_node; new_node->next = head; } return head; } linklistp cheak_node(linklistp head, int data) { if (head == nullptr) return nullptr; else { linklistp temp = head; while (temp->next != head&&temp->num != data) temp = temp->next; if (temp->num == data) return temp; return nullptr; } } linklistp delete_node(linklistp head, int data) { if (head == nullptr) { return nullptr; } linklistp temp = head->next; linklistp pre = head; while (temp != head&&temp->num != data) { pre = temp; temp = temp->next; } if (temp->num != data)//当temp的值不为data说明没找到data return nullptr; else { if (temp->num == head->num)//判断temp是否为头节点 { pre->next = temp->next; free(temp); return pre->next; } else { pre->next = temp->next; free(temp); } return head; } } linklistp replace_node(linklistp head, int data, int new_data)//用新节点替换data数据 { if (head == nullptr) { return nullptr; } linklistp temp = head->next; linklistp pre = head; while (temp != head&&temp->num != data) { pre = temp; temp = temp->next; } if (temp->num != data)//当temp的值不为data说明没找到data return nullptr; else { linklistp new_node = (linklistp)malloc(sizeof(linklist)); new_node->num = new_data; new_node->next = nullptr; if (temp->num == head->num)//判断temp是否为头节点 { pre->next = new_node; new_node->next = temp->next; free(temp); return pre->next; } else { pre->next = new_node; new_node->next = temp->next; free(temp); } } return head; } linklistp creat_list() { linklistp head =nullptr; srand(time(nullptr)); for (int i = 0; i < 8; i++) { linklistp new_node = (linklistp)malloc(sizeof(linklist)); new_node->num = rand() % 100; new_node->next = nullptr; head = insert_node(head, new_node); output(head); getchar(); } return head; } int main() { linklistp head = creat_list(); int data; linklistp temp = nullptr; printf("please input a data for cheak."); scanf_s("%d",&data); temp = cheak_node(head, data); if (temp) printf("Yes"); else printf("No"); printf("\n"); printf("please input a data for delete."); printf("\n"); scanf_s("%d", &data); temp = delete_node(head, data); if (temp) { head = temp; output(head); } else printf("No"); printf("\n"); printf("please input num for replace"); printf("\n"); scanf_s("%d", &data); printf("please input replace data"); int new_data = 0; scanf_s("%d", &new_data); temp = replace_node(head, data, new_data); if (temp == nullptr) { printf("Can't seach data for replace"); } else { head = temp; output(head); } return 0; }
双向链表
#include<stdio.h> #inc 4000 lude<stdlib.h> #include<time.h> typedef struct Dlinklist { int num; Dlinklist *next; Dlinklist *pre; }Dlinknode, *Dlinklistp; void output(Dlinklistp head) { Dlinklistp temp = head; do { printf("%d ", temp->num); temp = temp->next; } while (temp->next); printf("\n"); do { printf("%d ", temp->num); temp = temp->pre; } while (temp); } Dlinklistp insert_node(Dlinklistp head, Dlinklistp new_node) { if (new_node == nullptr) return head; if (head == nullptr) head = new_node; else { Dlinklistp temp = head; while (temp->next!=nullptr) temp = temp->next; temp->next = new_node; new_node->pre = temp; } return head; } Dlinklistp cheak_node(Dlinklistp head, int data) { Dlinklistp temp = head; while (temp != nullptr&&temp->num != data) { temp = temp->next; } if (temp == nullptr) return nullptr; return temp; } Dlinklistp delete_node(Dlinklistp head, int data) { if (head == nullptr) { return nullptr; } Dlinklistp temp = head; if (temp->num == data) { temp = temp->next; temp->pre = nullptr; free(head); return temp; } while(temp!=nullptr&&temp->num!=data) temp = temp->next; if (temp == nullptr) return nullptr; if (temp->next == nullptr) { temp->pre->next = nullptr; return head; } temp->pre->next = temp->next; temp->next->pre = temp->pre; free(temp); return head; } Dlinklistp replece_node(Dlinklistp head, int data, int new_data) { Dlinklistp temp = head; if (temp) { Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinklist)); new_node->num = new_data; new_node->next = nullptr; new_node->pre = nullptr; if (temp->num == data) { new_node->next = temp->next; temp->next->pre = new_node; free(temp); return new_node; } while (temp != nullptr&&temp->num != data) { temp = temp->next; } if (temp == nullptr) return nullptr; if (temp->next == nullptr)//当data在末尾时 { temp->pre->next = new_node; new_node->pre = temp->pre; free(temp); return head; } //非末尾 temp->pre->next = new_node; new_node->next = temp->next; new_node->pre = temp->pre; temp->next->pre = new_node; free(temp); return head; } return nullptr; } Dlinklistp creat_Dlist() { Dlinklistp head = nullptr; srand(time(nullptr)); for (int i = 1; i <6; i++) { Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinknode)); new_node->num = rand() % 100; new_node->pre = nullptr; //将节点的pre赋值为空 new_node->next = nullptr;//将节点的next赋值为空 head = insert_node(head, new_node); output(head); getchar(); } return head; } int main() { Dlinklistp head = creat_Dlist(); int data; printf("please input a data for cheak"); printf("\n"); scanf_s("%d", &data); Dlinklistp temp = cheak_node(head,data); if (temp) printf("Yes"); else printf("No"); printf("\n"); printf("please input a data for delete"); printf("\n"); scanf_s("%d", &data); temp = delete_node(head, data); if (temp) { head = temp; output(head); } else printf("No"); printf("\n"); printf("please input a data for delete"); printf("\n"); scanf_s("%d", &data); int new_data; scanf_s("%d", &new_data); temp = replece_node(head, data,new_data); if (temp) { head = temp; output(head); } else printf("No"); printf("\n"); return 0; }
双向循环链表
#include<stdio.h> #include<stdlib.h> #include<time.h> typedef struct Dlinklist { int num; Dlinklist *next; Dlinklist *pre; }Dlinknode, *Dlinklistp; void output(Dlinklistp head) { Dlinklistp temp = head; if (temp) { printf("%d ", temp->num); while (temp->next != head) { temp = temp->next; printf("%d ", temp->num); } printf("\n"); while (temp != head) { printf("%d ", temp->num); temp = temp->pre; } printf("%d ", temp->num); } } Dlinklistp insert_node(Dlinklistp head, Dlinklistp new_node) { if (new_node == nullptr) return head; if (head == nullptr)//头部自己形成一个环 { head = new_node; new_node->next = new_node; new_node->pre = new_node; } else { Dlinklistp temp = head; while (temp->next != head)//遍历到与head相连的节点 temp = temp->next; temp->next = new_node; new_node->pre = temp; new_node->next = head; head->pre = new_node; } return head; } Dlinklistp cheak_node(Dlinklistp head, int data) { if (head == nullptr) return nullptr; else { Dlinklistp temp = head; while(temp->next!=head&&temp->num!=data) temp = temp->next; if (temp->num == data) return temp; return nullptr; } } Dlinklistp delete_node(Dlinklistp head, int data) { Dlinklistp temp = head; if (temp) { if (temp->num==data)//因为头部删除不能返回head, 所以分类改变他的返回值 { temp->pre->next = temp->next; temp->next->pre = temp->pre; temp = temp->next; free(head); return temp; } while (temp->next != head&&temp->num != data) temp = temp->next; if (temp->num == data) { temp->pre->next = temp->next; temp->next->pre = temp->pre; return head; } } return nullptr; } Dlinklistp replece_node(Dlinklistp head, int data, int new_data)//将已有的data用新的节点替换成new_data { Dlinklistp temp = head; if (temp)//判断temp是否为空 { Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinklist)); new_node->num = new_data; new_node->next = nullptr; new_node->pre = nullptr; if (temp->num == data)//因为头部替换不能返回head,所以分类改变他的返回值 { temp->pre->next = new_node; new_node->pre = temp->pre; new_node->next = temp->next; temp->next->pre = new_node; free(head); return new_node; } while (temp->next != head&&temp->num != data)//遍历查找 temp = temp->next; if (temp->num == data) { temp->pre->next = new_node; new_node->pre = temp->pre; new_node->next = temp->next; temp->next->pre = new_node; free(temp); return head; } } return nullptr; } Dlinklistp creat_Dlist() { Dlinklistp head = nullptr; srand(time(nullptr)); for (int i = 1; i <6; i++) { Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinknode)); new_node->num = rand() % 100; new_node->pre = nullptr;//将节点的pre赋值为空 new_node->next = nullptr;//将节点的next赋值无空 head = insert_node(head, new_node); output(head); getchar(); } return head; } int main() { Dlinklistp head = creat_Dlist(); int data; printf("please input a data for cheak"); printf("\n"); scanf_s("%d", &data); Dlinklistp temp = cheak_node(head, data); if (temp) printf("Yes"); else printf("No"); printf("\n"); printf("please input a data for delete"); printf("\n"); scanf_s("%d", &data); temp = delete_node(head, data); if (temp) { head = temp; output(head); } else printf("No"); printf("\n"); printf("please input a data for replece"); printf("\n"); scanf_s("%d", &data); int new_data; scanf_s("%d", &new_data); temp = replece_node(head, data, new_data); if (temp) { head = temp; output(head); } else printf("No"); printf("\n"); return 0; }
数据结构内存对齐
结构(struct)的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragma pack指定的数值和这个数据成员自身长度中,比较小的那个进行。例如。#include<stdio.h> #include<stdlib.h> #pragma pack(1) /* n = 1, 2, 4, 8, 16 */ struct test { int a; char b; short c; char d[6]; }; int main() { printf("%d \n", sizeof(int)); printf("%d \n", sizeof(char)); printf("%d \n", sizeof(short)); printf("%d \n", sizeof(test)); return 0; }
当为pack(1)时,依次排列,test占13个字节,当pack(2)是int a占4个字节,char b占1个字节后因为short b为2个字节大于剩下的 所以此时char b后面空个了1个字节,说最后test占14个字节。
二叉排序树
暂无hash散列
哈希表(Hash table,也叫散列表),是根据key而直接进行访问的数据结构。也就是说,它通过把key映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。 哈希表的做法其实很简单,就是把key通过一个固定的算法函数即所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里。队列 栈 链表
链表是一种存储结构,指的是存储时候除了要存储数据元素外,还要数据元素一起的另外空间存储数据元素的关系队列和栈都是线性表,属于逻辑结构范畴,都是访问点受到限制,并且限制在线性表端点的线性表
栈被限定为在线性表的同一个(唯一的一个)端点插入删除(先进后出)。
队列被限定为在线性表的一端插入,另外一个端点删除(先进先出)。
相关文章推荐
- LeetCode-3-Longest Substring Without Repeating Characters
- Java数据结构:栈的实现
- 【19】-快速排序详解
- 不明觉厉的数据结构题2
- Python数据结构--Python学习笔记四
- 【数据结构】二叉树遍历
- 【数据结构与算法】十六 二叉树遍历 BFS 广度优先 递归算法
- Java千百问_06数据结构(004)_java数据类型的字面值是什么
- 二叉树的线索化算法思想详解
- 【数据结构与算法】十五 二叉树遍历 DFS 深度优先 递归算法
- 时间复杂度和空间复杂度详解
- 数据结构
- 数据结构之链表学习心得
- 数据结构之队列
- Java千百问_06数据结构(003)_什么是基本类型包装器
- 回溯法 -数据结构与算法
- 回溯法 -数据结构与算法
- 二叉树遍历 - 数据结构
- 二叉树遍历 - 数据结构
- 数据结构- 串的模式匹配算法:BF和 KMP算法