算法与数据结构设计

5.20:线性表

#include <stdio.h>

// 定义线性表中最大的数据量
#define Max_Size 50

// 定义线性表结构体
typedef struct
{
    int data[Max_Size];
    int length;
}SqList;


// 初始化线性表
bool InitList(SqList &L){
    L.length = 0;
    return true;
}


// 插入数据
bool ListInsert(SqList &L,int i,int e){
    if (L.length >=Max_Size)
    {
        printf("线性表已满\n");
        return false;
    }
    if (i<1 || i>L.length+1)
    {
        printf("插入位置不合法\n");
        return false;
    }
    for(int j = L.length;j >= i;j--){
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = e;
    L.length++;
    return true;
}

int main()
{
    SqList L;
    InitList(L);
    if (ListInsert(L,3,10))
    {
        printf("插入成功\n");
    }
    else{
        printf("插入失败\n");
    }
    
    return 0;
}
#include <stdio.h>

// 定义线性表中最大的数据量
#define Max_Size 50

// 定义线性表结构体
typedef struct
{
    int data[Max_Size];
    int length;
}SqList;


// 初始化线性表
bool InitList(SqList &L){
    L.length = 0;
    return true;
}


int main()
{
    SqList L;
    if(InitList(L)){
        printf("线性表初始化成功!\n");
    };
    return 0;
}
#include <stdio.h>
#include "common.h"



// 插入数据
bool ListInsert(SqList &L,int i,int e){
    if (L.length >=Max_Size)
    {
        printf("线性表已满\n");
        return false;
    }
    if (i<1 || i>L.length+1)
    {
        printf("插入位置不合法\n");
        return false;
    }
    for(int j = L.length;j >= i;j--){
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = e;
    L.length++;
    printf("线性表长度为:%d\n",L.length);
    return true;
}


// 删除数据
bool ListDelete(SqList &L,int i ){
    if (L.length == 0)
    {
        printf("线性表为空\n");
        return false;
    }
   if (i < 1 || i > L.length)
   {
        printf("删除的位置不合法\n");
        return false;
   }
   int e;
   e = L.data[i-1];
   printf("删除的元素是:%d\n",e);
   for (int j = i; j <= L.length; j++)
   {
    L.data[j-1] = L.data[j];
   }
   L.length--;
   printf("线性表长度为:%d\n",L.length);
   return true;
    
}

// 打印线性表
void PrintList(SqList L){
    for (int i = 0; i < L.length; i++)
    {
        printf("线性表中的元素:%d ",L.data[i]);
    }
    printf("\n");
}


// 按位查找
// 按值查找
int main()
{
    SqList L;
    InitList(L);
    for (int i = 1; i < 10; i++)
    {
        ListInsert(L,i,i);
    }
    PrintList(L);
    ListDelete(L,3);
    PrintList(L);
    return 0;
}
#include <stdio.h>

// 定义线性表中最大的数据量
#define Max_Size 50

// 定义线性表结构体
typedef struct
{
    int data[Max_Size];
    int length;
}SqList;


// 初始化线性表
bool InitList(SqList &L){
    L.length = 0;
    return true;
}

5.21:单链表

上午

List_HeadInsert.h
#include <stdio.h>

bool List_HeadInsert(LinkList &L){
    LNode *s = (LNode*)malloc(sizeof(LNode));
    int x;
    printf("请输入数据:");
    scanf("%d",&x);
    while (x != 999)
    {
        s ->data = x;
        S ->next = L->next;
        L ->next = s;
        scanf("%d",&x);
    }
    
    return true; //插入成功
}
travelList.h
#include <stdio.h>


int travelList(LinkList L){
    LinkList p = L->next;
    while (p !=NULL)
    {
        printf("单链表中的值:%d\n",p->data);
        p = p->next;//指向下一个节点
    }
    
}
LinkList.cpp
#include <stdio.h>
#include "LinkListInit.h"
//#include "LinkListInsert.h"
#include "travelList.h"
#include "LinkListDelete.h"
#include "List_HeadInsert.h"

int main()
{
    LinkList L;
    if (InitList(L))
    {
        printf("单链表初始化成功\n");
        //for (int i = 0; I < 10; i++)
        //{
            //if(InsertList(L,i+1,i+1)){
                //printf("数据%d插入成功!\n",i+1);
            //};
        //}
        List_HeadInsert(L);
        travelList(L);
        deleteLinkList(L,5);
        travelList(L);
    }
    
    return 0;
}
LinkListInsert.h
#include <stdio.h>

bool InsertList(LinkList &L,int i,int e){
    LNode *p = L; //指针p指向当前扫描到的节点
    int j = 0; //记录当前p指向的节点是第几个
    while (p != NULL && j < i-1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL || j > i-1){
        printf("插入位置不合法\n");
        return false;
    }
    /*申请一个临时地址*/
    LNode *s = (LNode*)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
    
}
LinkListDelete.h
#include <stdio.h>

bool deleteLinkList(LinkList &L,int i,){
    LNode *p = L;
    int j = 0;
    while (p !=NULL && j < i-1)
    {
        p = p->next;
        j++;
    }
    if (!(p->next) || j > i-1)
    {
        printf("删除位置不合法!\n");
        return false;
    }
    LNode *q = p->next
    p->next = q->next;
    free(q);//释放
    printf("删除的元素为:%d\n",q->date);
    return true;

}

下午

StackPop.h
#include <stdio.h>

bool Pop(SqStack &S,int &e){
    if (S.top == -1)
    {
       printf("栈空\n");
       return false;
    }
    e = S.data[S.top--];
    return true;
}


bool GetTop(SqStack S,int &e){
    if (S.top == -1)
    {
       printf("栈空\n");
       return false;
    }
    e = S.data[S.top];
    return true;
}

StackInit.h
#include <stdio.h>
#define Max_size 100

typedef struct{
    int data[Max_size];
    int top;
}SqStack;

void InitStack(SqStack &S){
    S.top = -1; //初始化栈顶指针为-1
    printf("栈初始化成功!\n");
}
StackEmpty.h
#include <stdio.h>

bool StackEmpty(SqStack S){
    if (S.top == -1)
    {
        return false;
    }else{
        return true; 
    }
    
}
StackList.cpp
#include <stdio.h>
#include "StackInit.h"
#include "StackEmpty.h"
#include "StackPush.h"
#include "StackPop.h"

int main()
{
    SqStack S;
    InitStack(S);
    if (!StackEmpty(S))
    {
        printf("栈为空!\n");
    }else{
        printf("栈不为空!\n");
    }
    for (int i = 0; i < 10; i++)
    {
        Push(S,i);
    }
    if (!StackEmpty(S))
    {
        printf("栈为空!\n");
    }else{
        printf("栈不为空!\n");
    }
    int e;
    Pop(S,e);
    printf("出栈元素为%d\n",e);
    Pop(S,e);
    printf("出栈元素为%d\n",e);
    Pop(S,e);
    printf("出栈元素为%d\n",e);
    int i;
   GetTop(S,i);
   printf("栈顶元素为%d\n");
    return 0;
}
StackPush.h
#include <stdio.h>

bool Push(SqStack &S,int e){
    if (S.top == Max_size -1)
    {
       printf("栈满\n");
       return false;
    }else{
        S.data[++S.top]=e;
        printf("元素%d入栈成功\n",e);
        return true;
    }
    
}
demo.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// 定义单链表的结构体
typedef struct LNode
{
    char data;
    struct LNode *next;
} LNode, *LinkList;

// 初始化单链表
bool InitList(LinkList &L)
{
    // 给单链表申请空间存储
    L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    return true;
}



// 插入字符节点
bool Insert(LinkList &L,char c)
{
    LNode *p = L;
    LNode *s = (LNode *)malloc(sizeof(LNode));
    s->data = c;
    s->next = p->next;
    p->next = s;
    return true;
}

int travleList(LinkList L){
    LinkList p = L->next;
    while (p !=NULL)
    {
        printf("单链表中的值:%c\n",p->data);
        p = p->next; // 指向下一个节点
    }
    printf("\n");
    return 0;
}

int dc(LinkList &L,int n){
    int i;
    char s[n/2];
    LNode *p = L->next;
    for(i = 0;i < n/2;i++){
        s[i] = p->data;
        p = p->next;
    }
    i--;
    if (n%2 == 1)
    {
        p = p->next;
        };
        while (p!=NULL&&s[i]==p->data)
        {
            i--;
            p = p->next;
        }
        if (i==-1)
        {
            return 1;
        }else{
            return 0;
        }
        
    
    
    
}


int main()
{
    LinkList L;
    int e;
    InitList(L);
    Insert(L,'x');
    Insert(L,'y');
    Insert(L,'y');
    Insert(L,'x');
    travleList(L);
    if(dc(L,4)==1){
        printf("链表中心对称");
    }else{
        printf("链表不是中心对称");
    };
    return 0;
}

5.22:

队列

DuiLie.h
#include <stdio.h>
#define MAX_Size 10


typedef struct{
    int data[MAX_Size];
    int front;
    int rear;
}SqQueue;

void InitSqQueue(SqQueue &S){
    S.front=S.rear = 0;
    printf("队列初始化成功\n");
}


bool EnQueue(SqQueue &S,int e){
    // 入队前先判定是否队满
    if((S.rear +1)%MAX_Size == S.front){
        printf("元素%d入队时队满!\n",S.data[S.rear]);
        printf("队满\n");
        return false;
    }
    S.data[S.rear] = e;
    printf("元素%d入队成功!\n",S.data[S.rear]);
    S.rear = (S.rear +1)%MAX_Size;
    printf("队尾指针后移%d\n",S.rear);
    return true;
}
DuiLie.cpp
#include <stdio.h>
#include "DuiLie.h"

int main()
{
    SqQueue S;
    InitSqQueue(S);
    for (int i = 0; i < 10; i++)
    {
        EnQueue(S,i);
    }
    

    return 0;

}
2.0h
#include <stdio.h>
#define Max_Size 10

typedef struct 
{
    int data[Max_Size];
    int front;
    int rear;
}SqQueue;

void InitSqQueue(SqQueue &S){
    S.front = S.raer = 0;
    printf("队列初始化成功\n");
}

bool EnQueue(SqQueue &S,int e){
     //入队前先判定是否队满

     if ((S.rear+1)%Max_Size == S.front)
     {
        print("元素%d入队时队满!\n",S.data[S.rear]);
        printf("队满\n");
        return false;
     }
     S.data[S.rear] = e;
     printf("元素%d入队成功\n",S.data[S.rear]);
     S.rear = (S,rear +1)%Max_Size;
     printf("队尾指针后移%d\n",S.rear);
     return true;
}

bool DelQueue(SqQueue &S,int &e){
    if (S.front == S.rear)
    {
        printf("队空\n");
        return false;
    }
    e = S.data[S.front];
    S.front = (S.front+1)%Max_Size;
    return true;
}

int LenQueue(SqQueue S){
     return (S.rear-S.front+Max_Size)%Max_Size;
}
2.0cpp
#include <stdio.h>
#include "DuiLie.h"

int main()
{
    SqQueue S;
    InitSqQueue(S);
    for (int i = 0; i < 10; i++)
    {
        EnQueue(S,i);
        printf("队列长度为:%d:\n",LenQueue(S));
    }
    
    int e;
    for (int i = 0; i < 10; i++)
    {
        if(DelQueue(S,e)){
           printf("元素%d出队成功\n",e); 
           printf("队列长度为:%d:\n",LenQueue(S));
        }
    
    }
    
    
    

    return 0;

}
Dui.cpp
#include <stdio.h>
#include <stdlib.h>

// 定义节点的结构体
typedef struct LNode
{
   int data;
   struct LNode *next;
}LNode;

// 定义队列的结构体
typedef struct{
    LNode *front;
    LNode *rear;
}Squeue;

// 初始化队列
void InitQueue(Squeue &S){
    S.front = S.rear = (LNode *)malloc(sizeof(LNode));
    S.front->next = NULL;
    printf("队列链式存储初始化成功!\n");
}

// 判定队列是否为空
bool IsEmpty(Squeue S){
    if(S.front == S.rear){
        return true;
    }else{
        return false;
    }
}

// 数据元素进入队列
bool EnQueue(Squeue &S,int e){
    LNode *p = (LNode *)malloc(sizeof(LNode));
    p->data = e;
    p->next = NULL;
    S.rear->next = p;
    S.rear = p;
    return true;
}

// 数据元素出队
bool DeQueue(Squeue &S,int &e){
    if(S.front == S.rear){
        return false;
    }
    LNode *p = S.front->next;
    e = p->data;
    S.front->next = p->next;
    if(S.rear == p){
        S.rear = S.front;
    }
    free(p);
    return true;
}

int main()
{
    Squeue S;
    InitQueue(S);
    if(IsEmpty(S)){
        printf("队列为空!\n");
    }else{
        printf("队列不为空!\n");
    }
    for (int i = 0; i < 10; i++)
    {
        if(EnQueue(S,i)){
            printf("元素%d入队成功!\n",i);
        };
    }
    if(IsEmpty(S)){
        printf("队列为空!\n");
    }else{
        printf("队列不为空!\n");
    }
    int e;
    for (int i = 0; i < 11; i++)
    {
        if(DeQueue(S,e)){
            printf("出队元素为%d\n",e);
            }else{
                printf("队列为空!\n");
            };
        
    }
    
    
    
    return 0;
}

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

// 定义最大节点数
#define Max_Size 100

typedef struct
{
    int data[Max_Size];
    int nodeCount;
}SqBinaryTree;


SqBinaryTree *createEmptyTree(){
    SqBinaryTree *tree = (SqBinaryTree *)malloc(sizeof(SqBinaryTree));
    tree->nodeCount = 0;
    return tree;
}

void insertTree(SqBinaryTree *tree,int value){
    if(tree->nodeCount >= Max_Size){
        printf("Tree is full!");
        return;
    }
    tree->data[tree->nodeCount] = value;
    tree->nodeCount++;
}

void preOrderTree(int data[],int start,int end){
    for (int i = 0; i < end - start + 1; i++)
    {
        printf("%d ",data[start+i]);
    }
    
}
Tree.cpp
#include <stdio.h>
#include "Tree.h"


int main()
{
    SqBinaryTree *tree;
    tree = createEmptyTree();
    for (int i = 0; i < 10; i++)
    {
        insertTree(tree, i);
    }
    preOrderTree(tree->data,0,tree->nodeCount-1);
    
    return 0;
}

5.23

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

typedef struct BiTNode
{
    int data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
} BiTNode, *BiTree;

BiTNode *insertBiTNode(BiTNode *root, int data)
{
    if (root == NULL)
    {
        BiTNode *tree = (BiTNode *)malloc(sizeof(BiTNode));
        tree->data = data;
        tree->lchild = NULL;
        tree->rchild = NULL;
        return tree;
    }
    if (data == root->data)
    {
        return root;
    }
    if (data < root->data)
    {
        root->lchild = insertBiTNode(root->lchild, data);
    }
    else
    {
        root->rchild = insertBiTNode(root->rchild, data);
    }
    return root;
}

// 先序遍历
void preOrderTraverse(BiTree T)
{
    if (T)
    {
        
        preOrderTraverse(T->lchild);
        
        preOrderTraverse(T->rchild);
        printf("%d ", T->data);
    }
}
tree.cpp
#include <stdio.h>
#include "tree.h"

int main()
{
    BiTNode *root = NULL;
    root = insertBiTNode(root,5);
    for (int i = 1;i<10; i++)
    {
        root = insertBiTNode(root,i);
    }
    preOrderTraverse(root);
    
    return 0;
}

二叉树

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


// 线索二叉树
// 1. 线索二叉树是二叉树的一种特殊形态,其节点中增加指向其双亲的线索,
// 2. 线索二叉树中所有空指针均被改为指向前驱或后继的线索,
// 3. 线索二叉树中所有节点中,其左指针域或右指针域为空,则该指针域指向其双亲节点,


typedef struct ThreedNode
{
    int data;
    struct ThreedNode *lchild,*rchild;
    int ltag,rtag;
    //ltag=0,rtag=0 表示左右孩子
    //ltag=1,rtag=0 表示左孩子
    //ltag=0,rtag=1 表示右孩子
    //ltag=1,rtag=1 表示双亲
}ThreedNode,*ThreedTree;


// 中序线索化函数
void inThreeding(ThreedTree p,ThreedTree *pre){
    if (p)
    {
        inThreeding(p->lchild,pre);
        if (!p->lchild)
        {
            p->lchild = *pre;
            p->ltag = 1;
        }
        if (*pre && !(*pre)->rchild)
        {
            (*pre)->rchild = p;
            (*pre)->rtag = 1;
        }
        *pre = p;
        inThreeding(p->rchild,pre);
    }
    
}


void inOrder(ThreedTree root){
    ThreedNode *p = root;
    while (p)
    {
        while (p->ltag == 0)
        {
            p = p->lchild;
        }
        printf("%d",p->data);
        while (p->rtag == 1 && p->rchild)
        {
            p = p->rchild;
            printf("%d",p->data);
        }
        p = p->rchild;
    }
    
}


ThreedNode *createNode(int data){
    ThreedNode *p = (ThreedNode*)malloc(sizeof(ThreedNode));
    p->data = data;
    p->lchild = p->rchild = NULL;
    p->ltag = p->rtag = 0;
    return p;
}
int main()
{
    ThreedTree root = createNode(1);
    root->lchild = createNode(2);
    root->rchild = createNode(3);
    root->lchild->lchild = createNode(4);
    root->lchild->rchild = createNode(5);
    root->rchild->lchild = createNode(6);
    root->rchild->rchild = createNode(7);
    ThreedNode *pre = NULL;
    inThreeding(root,&pre);
    printf("done");
    inOrder(root);
    return 0;
}

哈夫曼树

HuffmanTree.cpp
#include <stdio.h>

typedef struct
{
    int weight;
    int parent,left,right;
    
}HTNode,*HuffmanTree;

void Select(HuffmanTree HT,int end,int *s1,int *s2){
    int min1,min2;
    int i =1;
    while (HT[i].parent!=0&&i<=end)
    {
        i++;
    }
   min1 = HT[i].weight;
   *s1 = i;
   i++;
   while (HT[i].parent!=0&&i<=end)
   {
       i++;
   }
    if (HT[i].weight<min1)
    {
        min2  = min1;
        *s2 = *s1;
        min1 = HT[i].weight;
        *s1 = i;
    }
    else
    {
        min2 = HT[i].weight;
        *s2 = i;
    }
    for (int j = i+1; i <= end; j++)
    {
        if (HT[j].parent != 0)
        {
            continue;
        }
        if (HT[j].weight<min1)
        {
            min2 = min1;
            min1 = HT[i].weight;
            *s2 = *s1;
            *s1 = j;
        }
        else if(HT[j].weight >= min1&&HT[j].weight<=min2){
            min2 = HT[j].weight;
            *s2 = j;
        } 
    }
}
2.0
#include <stdio.h>
#include <stdlib.h>
//哈夫曼树结点结构
typedef struct {
    int weight;//结点权重
    int parent, left, right;//父结点、左孩子、右孩子在数组中的位置下标
}HTNode, *HuffmanTree;
//HT数组中存放的哈夫曼树,end表示HT数组中存放结点的最终位置,s1和s2传递的是HT数组中权重值最小的两个结点在数组中的位置
void Select(HuffmanTree HT, int end, int *s1, int *s2)
{
    int min1, min2;
    //遍历数组初始下标为 1
    int i = 1;
    //找到还没构建树的结点
    while(HT[i].parent != 0 && i <= end){
        i++;
    }
    min1 = HT[i].weight;
    *s1 = i;
   
    i++;
    while(HT[i].parent != 0 && i <= end){
        i++;
    }
    //对找到的两个结点比较大小,min2为大的,min1为小的
    if(HT[i].weight < min1){
        min2 = min1;
        *s2 = *s1;
        min1 = HT[i].weight;
        *s1 = i;
    }else{
        min2 = HT[i].weight;
        *s2 = i;
    }
    //两个结点和后续的所有未构建成树的结点做比较
    for(int j=i+1; j <= end; j++)
    {
        //如果有父结点,直接跳过,进行下一个
        if(HT[j].parent != 0){
            continue;
        }
        //如果比最小的还小,将min2=min1,min1赋值新的结点的下标
        if(HT[j].weight < min1){
            min2 = min1;
            min1 = HT[j].weight;
            *s2 = *s1;
            *s1 = j;
        }
        //如果介于两者之间,min2赋值为新的结点的位置下标
        else if(HT[j].weight >= min1 && HT[j].weight < min2){
            min2 = HT[j].weight;
            *s2 = j;
        }
    }
}
//HT为地址传递的存储哈夫曼树的数组,w为存储结点权重值的数组,n为结点个数
HuffmanTree *CreateHuffmanTree(HuffmanTree *HT, int *w, int n)
{
    // printf("w=%d\n",*(w+2-1));
    if(n<=1) return 0; // 如果只有一个编码就相当于0
    printf("n=%d\n", n);
    int m = 2*n-1; // 哈夫曼树总节点数,n就是叶子结点
    *HT = (HuffmanTree) malloc((m+1) * sizeof(HTNode)); // 0号位置不用
    HuffmanTree p = *HT;
    // 初始化哈夫曼树中的所有结点
    for(int i = 1; i <= n; i++)
    {
        (p+i)->weight = *(w+i-1);
        printf("%d\n", (p+i)->weight);
        (p+i)->parent = 0;
        (p+i)->left = 0;
        (p+i)->right = 0;
    }
    //从树组的下标 n+1 开始初始化哈夫曼树中除叶子结点外的结点
    for(int i = n+1; i <= m; i++)
    {
        (p+i)->weight = 0;
        (p+i)->parent = 0;
        (p+i)->left = 0;
        (p+i)->right = 0;
    }
    //构建哈夫曼树
    for(int i = n+1; i <= m; i++)
    {
        int s1, s2;
        Select(*HT, i-1, &s1, &s2);
        (*HT)[s1].parent = (*HT)[s2].parent = i;
        (*HT)[i].left = s1;
        (*HT)[i].right = s2;
        (*HT)[i].weight = (*HT)[s1].weight + (*HT)[s2].weight;
        printf("%d\n",(*HT)[i].weight);
    }
    // printf("%d\n",HT->weight);
    return HT;
}



//打印哈夫曼树某一结点及其子树
void printHuffmanNode(HuffmanTree HT, int nodeIndex, int indent) {
    // 该处的nodeIndex可以替换为2*n-1
    if (nodeIndex <= 0 || nodeIndex >nodeIndex) {
        return;
    }

    for (int i = 0; i < indent; i++) {
        printf(" ");
    }

    printf("Node %d: Weight=%d, Parent=%d, Left=%d, Right=%d\n", nodeIndex, HT[nodeIndex].weight, HT[nodeIndex].parent, HT[nodeIndex].left, HT[nodeIndex].right);

    printHuffmanNode(HT, HT[nodeIndex].left, indent + 4);
    printHuffmanNode(HT, HT[nodeIndex].right, indent + 4);
}




int main()
{
    int w[] = {12,40,15,8,25};
    HuffmanTree HT;
    CreateHuffmanTree(&HT,w,5);
    printHuffmanNode(HT,9,3);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值