zoukankan      html  css  js  c++  java
  • 数据结构(一):顺序表与链表

    顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。

    #include "Linear_List.h"
    
    void Linear_List::ListInit(MyList L)
    {
        L.length = 0;
    }
    
    int Linear_List::ListLength(MyList L)
    {
        return L.length;
    }
    
    ElemType Linear_List::ListGet(MyList L, int i)
    {
        if ((i > 1)&&(i < L.length))
        {
            return L.data[i - 1];
        }
        else
        {
            printf("溢出错误");
            return -1;
        }
    }
    
    int Linear_List::ListLocate(MyList L, ElemType i)
    {
        int j = 1;
        while (j <= L.length)
        {
            if (i != L.data[j - 1])
            {
                j++;
            }
            else
            {
                break;
            }
        }
        return j;
    }
    
    ElemType Linear_List::ListPrior(MyList L, ElemType i)
    {
        int j = ListLocate(L, i);
        if (j == 1)
        {
            printf("无前驱");
            exit(0);
        }
        else
        {
            return L.data[j - 2];
        }
    }
    
    ElemType Linear_List::ListNext(MyList L, ElemType i)
    {
        int j = ListLocate(L, i);
        if (j == L.length)
        {
            printf("无后驱");
            exit(0);
        }
        else
        {
            return L.data[j];
        }
    }
    
    void Linear_List::ListInsert(MyList L, int j, ElemType i)
    {
        if (L.length == MAXSIZE)
        {
            printf("表满");
            exit(0);
        }
        if ((j < 0) || (j > L.length))
        {
            printf("j错误");
            exit(0);
        }
        for (int n = L.length - 1; n >= j - 1; n--)
            L.data[n + 1] = L.data[n];
        L.data[j - 1] = i;
        L.length++;
    }
    
    void Linear_List::ListDel(MyList L, int i)
    {
        if ((i < 0) || (i > L.length))
        {
            printf("i错误");
            exit(0);
        }
        for (int n = L.length - 1; n > i - 1; n--)
            L.data[n - 1] = L.data[n];
        L.length--;
    }
    
    bool Linear_List::ListEmpty(MyList L)
    {
        return !L.length;
    }
    
    void Linear_List::ListTraverse(MyList L)
    {
        if (L.length == 0)
            printf("表空");
        else
            for (int i = 1; i < L.length; i++)
                printf("%d", L.data[i]);
    }
    #pragma once
    #ifndef _LINEAR_LIST_
    #define _LINEAR_LIST_
    
    #define MAXSIZE        1024
    
    #include <stdio.h>
    #include <stdlib.h>
    typedef int ElemType;
    
    struct node
    {
        int data[MAXSIZE];        //存放线性表数组
        int length;                //线性表长度
    };
    
    typedef struct node MyList;
    
    struct Linear_List
    {
        void ListInit(MyList L);
        int ListLength(MyList L);
        ElemType ListGet(MyList L, int i);
        int ListLocate(MyList L, ElemType i);        //定位函数,i是数据
        ElemType ListPrior(MyList L, ElemType i);    //求前驱
        ElemType ListNext(MyList L, ElemType i);    //求后继
        void ListInsert(MyList L, int j, ElemType i);            //前插
        void ListDel(MyList L, int i);                //删除
        bool ListEmpty(MyList L);                    //判空
        void ListTraverse(MyList L);                //遍历
    
    };
    #endif

     以上是顺序表

    链表

    #include "MyLinkedList.h"
    
    void LinkedListInit1(LinkedList L)
    {
        L = NULL;
    }
    
    void LinkedListInit2(LinkedList L)
    {
        L = (LNode *)malloc(sizeof(LNode));
        if (L == NULL)
        {
            printf("error");
            exit(0);
        }
        L->next = NULL;
    }
    
    int LinkedListLength(LinkedList L)
    {
        LNode *p;
        p = L->next;
        int j = 0;
        while (p != NULL)
        {
            j++;
            p = p->next;
        }
        return j;
    }
    
    LinkedList LinkedListGet(LinkedList L, int i)
    {
        LNode *p;
        p = L->next;
        int j = 1;
        while ((p != NULL)&&(j < i))
        {
            p = p->next;
            j++;
        }
        return p;
    }
    
    // LNode LinkedListLocate(LinkedList L, ElemType e)
    // {
    //     LNode *p;
    //     p = L->next;
    //     j = 1;
    //     while ((p)
    //     {
    //     }
    // }
    #ifndef _MYLINKEDLIST_
    #define _MYLINKEDLIST_
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int ElemType;
    
    struct Node
    {
        ElemType data;
        struct Node *next;
    };
    
    typedef struct Node LNode;
    typedef struct Node *LinkedList;
    
    void LinkedListInit1(LinkedList L);
  • 相关阅读:
    OpenCV和PHP的人脸识别技术
    Nginx下防CC和DDOS攻击
    Centos 6.0 yum 更新源
    linux(centos) 常用命令
    在 Nginx 中配置 Zend Framework
    nginx虚拟主机配置实例
    mysql精简语句集
    nginx rewrite 参数和例子
    nginx 和 subversion
    ZendFramework,php编码规范
  • 原文地址:https://www.cnblogs.com/Vcanccc/p/5671621.html
Copyright © 2011-2022 走看看