zoukankan      html  css  js  c++  java
  • 线性表(gcc实现)

    线性结构

      ①存在一个唯一的被称为“第一个”的数据元素;

      ②存在一个唯一的被称为“最后一个”的数据元素;

      ③除第一个元素外,每个元素均有唯一一个直接前驱;

      ④除最后一个元素外,每个元素均有唯一一个直接后继

    线性表(Linear List)

      是由n(n≧0)个数据元素(结点)a1,a2,…an组成的有限序列。该序列中的所有结点具有相同的数据类型。其中数据元素的个数n称为线性表的长度。

      当n=0时,称为空表。

      当n>0时,将非空的线性表记作:(a1,a2,…an) ,a1称为线性表的第一个(首)结点,an称为线性表的最后一个(尾)结点。

    顺序存储:把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里。

    #ifndef _TYPE_H_
    #define _TYPE_H_
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define TRUE    1
    #define FALSE   0
    #define INIT_SIZE 20
    #define INCREMENT 10
    #define OVERFLOW  -2
    
    typedef int Boolean;
    typedef char ElemType;
    
    #endif
    type.h
    /********************************************************
        Author: ydp        Version: 1.0    Date: 2013/05/03    
        File name: SqList.c
        Description:
            sequence list.
        Function List:
            init():
        History:
            none
    *********************************************************/
    #include "type.h"
    
    typedef struct ArrayList
    {
      ElemType *elem;
      int       length;
      int       size;
    }SqList;
    
    Boolean initList(SqList *L);
    Boolean insertList(SqList *L,ElemType elem, int pos);
    Boolean delList(SqList *L,int pos);
    Boolean updateList(SqList *L,ElemType elem, int pos);
    int     findList(SqList *L,ElemType elem);
    Boolean destroyList(SqList *L);
    
    int main(void)
    {
      SqList L,*p;
      p=&L;
      Boolean flag =  initList(&L);
      ElemType e = 'y';
      flag = insertList(&L,e,1);
      e = 'p';
      insertList(&L,e,2);
      e = 'd';
      insertList(&L,e,2);
      printf("before delete:	%s
    ",p->elem);
      delList(&L,1);
      printf("L->elem:	%s
    ",p->elem);
      printf("L->elem[0]:	%c
    ",p->elem[0]);
      printf("L->size:	%d
    ",p->size);
      printf("L->length:	%d
    ",p->length);
      insertList(&L,e,1);
      updateList(&L,'y',1);
      printf("after update:	%s
    ",p->elem);
      int pos = findList(&L,'p');
      printf("pos:	%d
    ",pos);
      destroyList(&L);
      if(p->elem==NULL)
      {
        printf("L->elem==NULL
    "); 
      }
      return 0 ;
    }
    
    Boolean initList(SqList *L)
    {
        printf("Init sqList start!
    ");
        L->elem = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType));
        if(L->elem==NULL)
        {
            printf("Init sqList false!
    ");
            exit(OVERFLOW);
        }
    
        L->length = 0;
        L->size = INIT_SIZE;
        printf("Init sqList success!
    ");
        return TRUE;
    }
    
    Boolean insertList(SqList *L,ElemType elem,int pos)
    {
        printf("Insert list start!
    ");
        if(L->elem==NULL)
        {   
            printf("List is not init!
    ");
            initList(L);
        }else if(L->length >= L->size)
        {
            L->elem = (ElemType *) realloc (L->elem,(L->size+INCREMENT)*sizeof(ElemType));
            L->size = L->size + INCREMENT;
            if(L->elem == NULL)
            {
                exit(OVERFLOW);
            } 
        }
        if(pos<0 || pos > L->length+1)
        {
            printf("Error position!");
            return FALSE;
        }
        if(L->length > 0)
        {
            int len=L->length-1; 
            for(len ; len>=pos-1 ; len--){
                L->elem[len+1]=L->elem[len];
            }
        }
        L->elem[pos-1] = elem;
        ++L->length;
        printf("Insert list success: %c
    ",elem);
        return TRUE;
    }
     
    Boolean delList(SqList *L, int pos)
    {
        if(pos > L->length || pos < 1) 
        {
          printf("del error position!");
          return FALSE;
        }
        for(;pos<=L->length;pos++)
        {
            L->elem[pos-1]=L->elem[pos];
        }
        L->length--;
        return TRUE;
    }
    Boolean updateList(SqList *L,ElemType elem, int pos)
    {
        if(pos<0 || pos >= L->length)
        {
            printf("Update elem error place!");
            return FALSE;
        }
        L->elem[pos-1]=elem;
        return TRUE;
    }
    
    int     findList(SqList *L,ElemType elem)
    { 
        if(L->elem == NULL)
        {
            return -1;
        }
        int i=0;
        for(;i<L->length;i++)
        {
            if(elem == L->elem[i])
                return i+1;
        }
        return -1;
    }
    
    Boolean destroyList(SqList *L)
    {
       if(L->elem)
       {
           free(L->elem);
           L->elem=NULL;
           printf("Destroy line list success! 
    ");
       }
    }
    SqList.c

    链式存储

    #ifndef _LIST_H_
    #define _LIST_H_
    
    #define TRUE    1
    #define FALSE   0
    #define OVERFLOW  -2
    
    typedef int Boolean;
    typedef char ElemType;
    
    typedef struct ListNode
    {
        ElemType  elem;
        struct ListNode *next;
    }LNode;
    
    Boolean isEmpty(LNode *node);
    void    print(LNode *node);
    void    addList_tail(LNode *node,int num);
    void    addList_head(LNode *node,int num);
    ElemType getElem(LNode *L,int i);
    int  locateElem(LNode *L, ElemType key);
    Boolean    insertElem(LNode *L, ElemType key, int addr);
    Boolean modifyElem(LNode *L, ElemType key, int addr);
    Boolean deleteElem(LNode *L, int addr);
    #endif
    list.h
    #include"list.h"
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main(void)
    {
        LNode *p,*head;
        head=p=(LNode *)malloc(sizeof(LNode));
        head->next=NULL;
        if(isEmpty(p)){
            printf("List is null
    ");
        }else {
            printf("List is not null
    ");
        }
        addList_tail(head,5); 
    //    addList_head(head,5);
        print(head);
        ElemType e = getElem(head,2);
        printf("getElem(2)=%c
    ",e);
        int add = locateElem(head,'f');
        printf("locateElem(f)=%d
    ",add);
        if(insertElem(head,'i',2)){
            printf("insert(head,'i',2) sucess!
    ");
        }else{
        printf("insert(head,'i',2) fail!
    ");
        }
        print(head);
        if(modifyElem(head,'h',1)){
        printf("modifyElem(head,'h',1) success! 
    ");
        }else{
            printf("modifyElem(head,'h',1) success! 
    ");
        }
        print(head);
        if(deleteElem(head,1)){
        printf("deleteElem(head,1) sucess!
    ");
        }else{
            printf("deleteElem(head,1) sucess!
    ");
        }
        print(head);
    }
    
    Boolean isEmpty(LNode *node){
        if(node->next != NULL){
            return FALSE;
        }
        return TRUE;
    }
    
    void print(LNode *node){
        while(node->next != NULL){
           node=node->next;
            printf("elem:%c
    ",node->elem);
        }
    }
    
    void addList_tail(LNode *node,int num){
        LNode *p,*q;
        int i;
        p=node;
        char e;
        printf("will input %d elem!",num);
        for(i=1;i<=num;i++){
            q=(LNode *)malloc(sizeof(LNode));
            printf("
    please input the %dth elem:",i);
        scanf("%c",&e);
        //The follow 2line is clear ‘'
    ' in memery
        int c;
            while ((c=getchar()) != '
    ' && c != EOF);
        //fflush(stdin);
           q->elem=e;
            q->next=p->next;
             p->next=q;
        p=q;
        }
    }
    
    void addList_head(LNode *node,int num){
        LNode *p,*q;
        int i;
        p=node;
        char e;
        printf("will input %d elem!",num);
        for(i=1;i<=num;i++){
            q=(LNode *)malloc(sizeof(LNode));
            printf("
    please input the %dth elem:",i);
            scanf("%c",&e);        
            int c;
            while ((c=getchar()) != '
    ' && c != EOF);        
            q->elem=e;
            q->next=p->next;
            p->next=q;
          //p=q;
        }
    }
    
    ElemType getElem(LNode *L,int i)
    {
        int count=0;
        LNode *p;
        p=L;
        for(;count<i;count++)
        {
            if(p->next!=NULL){
                p=p->next;
            }
            else
            {
                printf("out of boundry!");
                return ;
            }
        }
        
        return p->elem;
    }
    
    int  locateElem(LNode *L, ElemType key)
    {
        int count=0;
        LNode *p;
        p=L;
        while(p->next != NULL)
        {
            p=p->next;
            count++;
            if(p->elem == key)
                return count;
        }
        return -1;
    }
    
    Boolean insertElem(LNode *L, ElemType key, int addr)
    {
        LNode *p,*q;
        p = L;
        int count;
        for(count=1;count<addr;count++)
        {
            if(p->next != NULL)
                p=p->next;
            else
            {
                printf("Wrong addr!");
                return FALSE;
            }
        }
        q=(LNode *)malloc(sizeof(LNode));
        q->next = p->next;
        p->next = q;
        q->elem = key;
        
        return TRUE;
    }
    Boolean modifyElem(LNode *L, ElemType key, int addr)
    {
        LNode *p;
        p=L;
        int count;
        for(count=0;count<addr;count++)
        {
            if(p->next != NULL)
                p=p->next;
            else
            {
                printf("Wrong addr!");
                return FALSE;
            }
        }
        p->elem = key;
        return TRUE;
    }
    Boolean deleteElem(LNode *L, int addr)
    {
        LNode *p,*q;
        p=L;
        int count;
        for(count=0;count<addr-1;count++)
        {
            if(p->next != NULL)
                p=p->next;
            else
            {
                printf("Wrong addr!");
                return FALSE;
            }
        }
        q = p->next;
        p->next =  q->next;
        free(q);
        return TRUE;
    }
    list.c
  • 相关阅读:
    作业七:用户体验设计案例分析
    作业五:需求分析
    作业四:结对编程 词频统计
    作业四:结对编程,词频统计
    作业三:词频统计
    作业2
    Github注册过程
    ArrayList的说明及简单用法
    Java类中成员变量、局部变量、静态变量的区别
    AspNetCore.SignalR的JwtBearer认证
  • 原文地址:https://www.cnblogs.com/ydpup/p/3255159.html
Copyright © 2011-2022 走看看