链表基本算法
链表的创建与初始化
node* init(){
node *p = (node*)malloc(sizeof(node)) ;//动态申请一个头结点(或者第一个元素结点)--
//用malloc(I think it can o save out of the method and it located in stack)
p->info =NULL;//
node *head ;//= (node*)malloc(sizeof(node));
for (int i = 0; i < 10; i++)
{
node *q = (node*)malloc(sizeof(node));//apply next node which previous node link node
// and sizeof(类型不是指针类型的)
q->info = i ;//p 的data
p->next = q ;//indicate it
if (i==0)
head = p ;
p = q ;
}
p->next = NULL ; 防止最后一个没有结点 去随机指向 造成遍历它的时候,,,,,死循环
return head ;//return head node or return first element
}
c语言插入链表,指定位置之前。
insert function about 865 C++
void insert(node *link,int y , int x ){
node *p = link ;
while (p!=NULL)
{
if (p->next->info==y)//找它的下一个结点是否为y
{
node *q = (node*)malloc(sizeof(node));//apply a node which it
//shoule be insert designating the location.
q->next = p->next ; //让插入的结点下一个指向那个要被插队的结点
q->info = x;
p->next = q ;//被插队的前一个结点指向插队的结点,
break ;//结束。
}
p=p->next ;
}
}
遍历及输出
void display(node *link){
node *p = link->next ;
int i = 0;
while (p!=NULL)
{
printf("%d\n",p->info);
p = p->next ;
}
}
图的基本操作
邻接表创建图
typedef struct node{
int adjvex;
struct node *next;
}edgenode; // bian
typedef struct vnode{
datatype vertex;
edgenode *firstedge;
}vertexnode; // jiedian
typedef struct{
vertexnode adjlist [m];
int n,e;
}adjgraph;
-------- 邻接表的初始化。
核心代码 ----》 头插法 ----like chain excel
s=(edgenode *)malloc(sizeof(edgenode));
s->adjvex=j;
s->next=g->adjlist[i].firstedge;
g->adjlist[i].firstedge=s;
二叉树非算法
二叉树
穿线树的目的是利用空指针的存储空间,建立周游线索。为了区分线索和指针,需在每个结点中增加两个标志位,分别标识左右指针域是实际指针还是线索。
平均查找长度
递归算法or一些简单算法
再用非递归遍历的时候不能将同时考虑有左右子树就进栈,,,,
具体的也还没弄完 2021年10月21日19:30:41二叉排序树
右节点>跟结点>左节点,而且所以都符合,
并且用栈的时候容易陷入死循环当中要好好解决。
void preNode(tree *root){
tree *p = root ;
stack *st ;
st->top = -1 ;
while(p!=NULL){
if(p->left!=NULL){
printf("%d",p->data);
st->elem[++st->top] = p ;
}
else if(p->left==NULL){
printf("%d",p->data) ;
if(p->right!=NULL){
p = p->right ;
continue;
}
else{
p = st->elem[st->top--] ;
p = p->right ;
continue ;
}
}
p = p->left;
}
}
把后面的弄到前面去数组算法
void remove(int *p,int locate){
int j = 0;
for (int i = 0; i < 7; i++)
{
if (i>=locate-1)
{
*(p+i) = *(p+i+1);
printf("%d\n",++j);
}
}
}
找到X 在链表中 并删除它,
bool deleteNode(LinkList node,int x){
if(node==NULL)
return false ;
else{
LinkList p = node ;
LinkList q ;
while(p->next!=NULL){
if(p->data==x){
q->next = q->next;
free(p) ;
//p->next = p->next->next ;
return true ;
}
q = p ;
p = p->next ;
}
}
}
基本语法
typedef char datatype ; 这个是前面是类名后面是修改的
#include <stdio.h>
#include<iostream>
#include<malloc.h>
#include<math.h>
#include"windows.h"
using namespace std ;
typedef char datatype ;
typedef struct node{
datatype data ;
struct node *left,*right ;
}bintnode;
typedef bintnode* tree ;
typedef struct stack{
int top ;
tree elem[20] ;
}stack ,*Stack;
tree creatNode(char data){
tree node = (tree)malloc(sizeof(bintnode)) ; //初始化根节点 a
node->data = data ;
node->left = NULL ;
node->right = NULL;
return node;
}
tree init(){
//这么重复的代码可以用 一个createNode 的函数 把它给包括进去,减轻代码量。
tree root = (tree)malloc(sizeof(bintnode)) ; //初始化根节点 a
root->data = 'a' ;
tree lchilrd = (tree)malloc(sizeof(bintnode)) ; //左右孩子 b l
lchilrd->data = 'b' ;
tree rchilrd = (tree)malloc(sizeof(bintnode)) ; //c r
rchilrd->data = 'c' ;
tree lrchilrd = (tree)malloc(sizeof(bintnode)) ; //d lr
lrchilrd->data = 'd' ;
tree rlchilrd = (tree)malloc(sizeof(bintnode)) ; //e rl
rlchilrd->data = 'e' ;
tree lrrchilrd = (tree)malloc(sizeof(bintnode)) ; //f rlr
lrrchilrd->data = 'f' ;
root->left = lchilrd ; //建立关联。父节点与子节点
root->right = rchilrd ;
lchilrd->right = lrchilrd ;
rchilrd->left = rlchilrd ;
lrchilrd->right = lrrchilrd;
lchilrd->left =NULL ; rchilrd->right = rchilrd->left =NULL ; //将叶子节点的子孙弄成NULL;
lrchilrd->left = lrchilrd->right = NULL ;
rlchilrd->left = rlchilrd->right = NULL ;
lrrchilrd->right = lrrchilrd->right = NULL;
return root ;
}
void midScanner(tree root){
tree p = root ;
Stack st ;
st->top = -1 ;
// while(p!=NULL&&++i<8){
// printf("%c\t",p->data);
// if(p->left!=NULL)
// st->elem[++(st->top)] = p ;
// else if(p->right!=NULL)
// st->elem[++(st->top)] = p ;
// if(p->left!=NULL)
// p = p->left ;
// else if(p->right!=NULL)
// p = p->right ;
// else
// p = st->elem[(st->top)--] ;
// printf("%d\n",st->top);
// }
st->elem[++(st->top)] = p ;
while(st->top!=-1){
p = st->elem[st->top--] ;
while (p!=NULL)
{
printf("%c\t",p->data);
if(p->right!=NULL)
st->elem[++st->top] = p->right ;
p = p->left ;
printf("%d\n",st->top);
}
}
}
int main(){
tree root ;
root = init();
//cout<<root->left->right->data << endl;
midScanner(root);
return 0;
}