数据结构

作者: 苏鑫的博客 | 来源:发表于2017-09-25 10:25 被阅读0次

线性表

作为线性结构的一种,线性表是一种最简单最常用的数据结构
在c语言c99标准以前是没有引用的所以按照数据结构书中的方法编写无限报错,最后换了一种思维方式所谓的数据结构,不是强调语言而是数据结构,所以思维对了实现方法是可以多种多样的。

#include<stdio.h>
#include<stdlib.h>
#define MAX_LEN 10
enum bool {false,true};
typedef int Status;
typedef struct{
    char data[MAX_LEN];
    int index;
    }List;

void InitList(List *,int,char []);
void ClearList(List *);
enum bool ListEmpty(List *);
void GetEmpty(List,int,char * );
int LocateElem(List,char);
void PriorElem(List,char,List *);
void NextElem(List,char,List *);
void ListInsert(List *,int,char);
void ShowList(List);

void InitList(List *l,int i,char c[]){
        l->index = i;
        for(int i=0;i<l->index;i++){
                l->data[i]=c[i];
            }
    }
void CleartList(List *l){
    int i =0;
    while(!ListEmpty(l)){
        l->data[i++] = '\0';
    }
        l->index = 0;
    }
enum bool ListEmpty(List *l){
    if(l->data[0]=='\0'&&l->index==0){
        return true;
        }
    return false;
    }
void GetEmpty(List l,int i,char *e){
        if(ListEmpty(&l)){
            printf("list is empty\n");
            return;
        }
        e = &l.data[i];
        printf("Get element : %c\n",l.data[i]);
    }
void ShowList(List l){
        printf("List elem:");
        for(int i=0;i<l.index;i++){
            printf("%c\t",l.data[i]);
        }
        printf("\n");
    }
int LocateElem(List l,char e){
    if(!ListEmpty(&l)){
        for(int i=0; i < l.index;i++){
            if(l.data[i]==e){
                printf("The %c at %d\n",e,i);
                return i;
                }
            }
            return -1;
        }
    return -1;
    }
void PriorElem(List l,char e,List *l1){
    if(!ListEmpty(&l)&&(LocateElem(l,e)+1)){
            int i = LocateElem(l,e);
            for(int j = 0; j<i;j++){
                l1->data[j]=l.data[j];
                }
            l1->index = i;
        }
    }
void NextElem(List l, char e, List *l1){
        if(!ListEmpty(&l)&&(LocateElem(l,e)+1)){
            int i = LocateElem(l,e);
            for(int j = l.index; j>=i;j--){
                l1->data[j]=l.data[j];
                }
            l1->index =l.index - i;
        }
    }
void ListInsert(List *l,int index,char e){
        if(0<index&&index<l->index&&l->index<MAX_LEN-1){
            for(int i = l->index; i>=index;i--){
                l->data[i] = l->data[i-1];
                }
            l->data[index] = e;
            l->index++;
        }
    }
int main(){
    List l={};
    int len = 5;
    char e1;
    char c[] = {'a','b','c','d','e'}; 
    InitList(&l,len,c);
    ShowList(l);
    GetEmpty(l,2,&e1);  
    LocateElem(l,'c');
    List l1 = {};
    NextElem(l,'b',&l1);
    ShowList(l1);
    List l2 = {};
    PriorElem(l,'d',&l2);
    ShowList(l2);
    ListInsert(&l,2,'j');
    ShowList(l);
    CleartList(&l);
    ShowList(l);
    }


相关文章

  • IOS开发_数据结构

    1、数据结构; 2、算法; 3、数据结构与算法; 1、数据结构; 1.1 概念: 数据结构:数据结构是计算...

  • py基础

    5Python集合容器 数据结构数据结构 一般将数据结构分为两大类: 线性数据结构和非线性数据结构。 线性数据结构...

  • 思维导图之数据结构+算法

    数据结构+算法 = 程序 数据结构比较 参考文章 数据结构与算法数据结构与算法(java)

  • 数据结构与算法分析:大纲]

    00数据结构与算法分析:大纲01数据结构:数组02数据结构:链表03数据结构:栈03数据结构:队列 本系列课程主要...

  • 数据结构:数组

    00数据结构与算法分析:大纲01数据结构:数组02数据结构:链表03数据结构:栈03数据结构:队列 数组 数组是一...

  • 数据结构—概述

    数据结构概述 数据结构概述:程序设计 = 数据结构 + 算法数据结构:数据元素之间存在所有特定关系的集合,数据结构...

  • OVS 源码分析整理

    OVS 核心代码 OVS 架构 OVS 主要的数据结构数据结构关系图主要的数据结构和数据结构的参数数据结构代码 d...

  • 01. 数据结构与算法绪论

    一、数据结构 1. 什么是数据结构 2. 数据结构的分类 3. 常用的数据结构 4. 数据结构的应用表现 二、算法...

  • 数据结构与算法 - 查找

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构数据结构...

  • C#之数据结构(上)

    数据结构 一般将数据结构分为两大类: 线性数据结构和非线性数据结构。 线性数据结构有: 线性表、栈、队列、串、数组...

网友评论

    本文标题:数据结构

    本文链接:https://www.haomeiwen.com/subject/yxazsxtx.html