线性表总结

基础理论

逻辑结构

  线性表: 顾名思义是逻辑上连续的一组先性单元的集合

  这样的逻辑结构 : 在计算机中有两种对应的存储结构 ( 顺序表 , 链表 )

顺序表 : ( 物理相邻或者相隔固定参数 )

  特点 : 逻辑结构的位序明显 , 可以随机存取 , 插入/删除操作困难 .

             一般利用高级语言数组实现 .

  顺序表定义 : typedef struct {

                             ElemType *elem ;               //存储空间首地址

                             int                length ;              //当前长度

                             int                listsize;              //当前分配存储的容量( 以sizeof(ElemType)为单位)

             }SqList ;

  借助高级语言数组定义 : typedef struct {

                                               int data[ LISTSiZE ] ;               // listsize 之前的 #define 定义

                                               int length ;                                // 当前长度

                                          }SqList ;

链表 : 通过指针实现

  特点 : 逻辑结构的位序不明显 , 必须按照顺序查找 , 插入/删除操作不需要移动元素 .

             一般利用指针实现 .

  头指针 : 指示链表中第一个结点的存储位置 ( 如果有头结点 , 则指向头结点 )

  头结点 : 第一个存储结点之前添加一个结点 ( 为了方便操作一致性 , 插入操作 )

  首结点 : 真正存储元素的第一个结点 .

  单链表定义 : typedef struct LNode {

                               ElemType    data ;

                               struct LNode *next ;

                          } LNode , *LinkList ;

  头结点的作用说明 , 如下例子 :

    // 单链表的插入操作 ( 建立单链表 , 逆序 )

    void CreateList_L ( LinkList l , int n ) {

        l = ( LinkList ) malloc( sizeof(LNode));              //头结点

        l->next = NULL ;

        for( i=n ; i>0; --i ){

            p = (LinkList)malloc(sizeof(LNode));            //生成新结点

            scanf( &p->data) ;

            p->next = l->next ;

            l->next = p ;                                                        //逆序插入

        }   

    }

  循环链表定义 : ( 循环链表也是个单链表 , 定义同单链表 , 只是表尾指针不为 NULL , 而是指向头结点, 判断时注意 )

  双向链表定义 : typedef struct DuLNode {

                                      ElemType          data ;

                                      struct DuLNode  * prior ;                  //指向前驱

                                      struct DuLNode * next ;                    //指向后继

                                } DuLNode , * DuLinkList ;

 

静态链表定义

利用数组模拟指针,存储。

typedef struct {
           ELEM_TYPE data;
           int cur;                              /* simulate pointer */
 
} component, slinklist[MAXSIZE];

02
14
23
31
4haha 

静态链表有点类似以上内容,这是一个一维数组,数组中存放的是结构体。前边的 0,1,2,3,4是编号。方便记忆,没有实际意义。最后边的数字。类似指针

 

实例推荐

  1.( 2.19 练习册 )

/* This program is for the practice book 2.19
** Author : kevin
** Date   : 2012/02/17
*/

#include"stdio.h"
#include"alloc.h"
#include"stdlib.h"

typedef struct LNode{
    int data ;              /*数据存储*/
    struct LNode *next ;    /*后继指针*/
}LNode , *LinkList ;


/*创建链表*/
void CreateLinkList( LinkList l , int n )
{
    int i ;
    LinkList p = NULL;
    /*l = (LinkList)malloc(sizeof(LNode));    建立头结点*/
    l->next = NULL ;
    for(i=n ;i>0; i--){
        p = (LinkList)malloc(sizeof(LNode));
        p->data = i*2 ;
        p->next = l->next ;
        l->next = p;
    }
}


/*显示链表*/
void ShowLinkList( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p){
        printf("%d,",p->data);
        p = p->next ;
    }
    printf("\n");
}

/*删除操作*/
void DelLinkList( LinkList l , int mink , int maxk )
{
    LinkList p , q ;
    q = l ;
    p = l->next ;
    while(p){
        if( p->data >=mink && p->data <= maxk ){
            q->next = p->next ;
            free(p);
        }
        else{
            q = p ;
        }
        p = q->next ;

    }
}

int main()
{
    LinkList m ;
    m = (LinkList)malloc(sizeof(LNode));  /*头结点,分配内存空间*/
    CreateLinkList(m , 10);
    ShowLinkList(m);
    DelLinkList(m,10,20);
    ShowLinkList(m);
    getchar();
    getchar();
}


 2.( 2.22 练习册 )

/* This program for the 2.22 ( parctice )
** Author : kevin
** Date   : 2012/02/17
*/

#include"stdio.h"
#include"stdlib.h"
#include"alloc.h"

typedef struct LNode{
    int data ;
    struct LNode *next ;
}LNode,*LinkList;


/* Create Link List */
void CreateLinkList( LinkList l , int n )
{
    int i ;
    LinkList p = NULL;
    l->next = NULL ;
    for(i=0 ;i<n; i++){
        p = (LinkList)malloc(sizeof(LNode));
        p->data = i*2 ;
        p->next = l->next ;
        l->next = p;
    }
}

/*show Link list*/
void ShowLinkList( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p){
        printf("%d,",p->data);
        p = p->next ;
    }
    printf("\n");
}

/*direct change , only change pointer */
void Nizhi(LinkList l)
{
    LinkList p , q , r ;
    q = l ;
    p = l->next ;
    r = p->next ;
    while(p){
        if(q==l){
            p->next = NULL;
        }
        else{
            p->next = q ;
        }
        q = p ;
        p = r ;
        r = r->next ;

    }
    l->next = q ;
}

int main()
{
    LinkList m ;
    m = (LinkList)malloc(sizeof(LNode));
    CreateLinkList(m,10);
    ShowLinkList(m);
    Nizhi(m);
    ShowLinkList(m);
    getchar();
}


 3. ( 2.24 练习册 )

/* This program is for 2.24( practice )
** Author : kevin
** Date   : 2012/02/17
*/

#include"stdio.h"
#include"stdlib.h"
#include"alloc.h"

typedef struct LNode{
    int data ;
    struct LNode *next ;
}LNode , *LinkList ;


/* Create Link List */
void CreateLinkList( LinkList l , int *a , int n )
{
    int i ;
    LinkList p = NULL;
    l->next = NULL ;
    for(i=0 ;i<n; i++){
        p = (LinkList)malloc(sizeof(LNode));
        p->data = *(a+i) ;
        p->next = l->next ;
        l->next = p;
    }
}

/*show Link list*/
void ShowLinkList( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p){
        printf("%d,",p->data);
        p = p->next ;
    }
    printf("\n");
}

/*The first change the position*/
void Nizhi(LinkList l)
{
    LinkList p , q , r ;
    q = l ;
    p = l->next ;
    r = p->next ;
    while(p){
        if(q==l){
            p->next = NULL;
        }
        else{
            p->next = q ;
        }
        q = p ;
        p = r ;
        r = r->next ;

    }
    l->next = q ;
}

/*Sort*/
void SortLinkList( LinkList a,LinkList b, LinkList c )
{
    LinkList p , q , r ;
    p = a->next ;
    q = b->next ;
    r = c
     ;
    while(p && q){
        if(p->data >= q->data){
            r->next = p;
            r = p ;
            p = p->next ;
        }
        else{
            r->next = q;
            r = q ;
            q = q->next;
        }

    }
    if(p){
        r->next = p ;
    }
    if(q){
       r->next = q ;
    }
}


int main()
{
    int data1[8] = { 20,15,12,9,8,6,3,1};
    int data2[7] = { 14,12,10,8,6,4,2};
    LinkList m , n , lm;
    m = (LinkList)malloc(sizeof(LNode));
    n = (LinkList)malloc(sizeof(LNode));
    lm = (LinkList)malloc(sizeof(LNode));
    CreateLinkList(m,data1,8) ;
    CreateLinkList(n,data2,7) ;
    ShowLinkList(m);
    ShowLinkList(n);
    Nizhi(m);
    Nizhi(n);
    SortLinkList( m,n,lm);
    ShowLinkList(lm);
    getchar();

}




 4. ( 2.30 练习册 )

/* This program is for 2.30 ( practice )
** Author : kevin
** Date   : 2012/02/17
*/


#include"stdio.h"
#include"alloc.h"
#include"stdlib.h"

typedef struct LNode{
    int data ;
    struct LNode *next ;
}LNode , *LinkList;



/* Create Link List */
void CreateLinkList( LinkList l , int *a , int n )
{
    int i ;
    LinkList p = NULL;
    l->next = NULL ;
    for(i=0 ;i<n; i++){
        p = (LinkList)malloc(sizeof(LNode));
        p->data = *(a+i) ;
        p->next = l->next ;
        l->next = p;
    }
}

/*show Link list*/
void ShowLinkList( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p){
        printf("%d,",p->data);
        p = p->next ;
    }
    printf("\n");
}

/*delete the difference between two link list*/
void DelDifferece( LinkList a , LinkList b)
{
    LinkList p , q ,r ;
    p = a->next ;
    q = b->next ;
    r = a ;
    while(p && q){
        if(p->data == q->data){
            r = p ;
            p = p->next ;
            q = q->next ;
        }
        else if( p->data > q->data){
            q = q->next ;
        }
        else{
            r->next = p->next ;
            free(p);
            p = r->next ;
        }
    }

}

/*delete the same between two link list*/
void DelSame( LinkList a, LinkList b)
{
    LinkList p , q , r ;
    p = a->next ;
    q = b->next ;
    r = b ;
    while( p && q){
        if(p->data == q->data){
            r->next = q->next ;
            free(q);
            q = r->next ;
            p = p->next ;
        }
        else if(p->data > q->data){
            r = q;
            q = q->next ;
        }
        else{
            p = p->next ;
        }
    }
}


int main()
{
    int data1[8] = { 20,15,12,9,8,6,3,1};
    int data2[7] = { 14,12,10,8,6,4,2};
    int data3[10] = {20,19,18,17,16,15,14,13,12,11};
    LinkList la , lb, lc;
    la = (LinkList)malloc(sizeof(LNode));
    lb = (LinkList)malloc(sizeof(LNode));
    lc = (LinkList)malloc(sizeof(LNode));
    CreateLinkList(la,data1,8) ;
    CreateLinkList(lb,data2,7) ;
    CreateLinkList(lc,data3,10) ;
    ShowLinkList(la);
    ShowLinkList(lb);
    ShowLinkList(lc);
    printf("--------------The Result is -------------\n");
    DelDifferece(la,lb);
    ShowLinkList(la);
    DelSame(la,lc);
    ShowLinkList(lc);
    getchar();

}




  5. ( 2.33 练习册 )

/* This program is for 2.33 ( practice )
** Author : kevin
** Date   : 2012/02/17
*/


#include"stdio.h"
#include"stdlib.h"
#include"alloc.h"

typedef struct LNode{
    char data ;             /* store the charactor */
    struct LNode *next ;    /* point the next */
}LNode, *LinkList;


/* Create Link List */
void CreateLinkList( LinkList l , char *a , int n )
{
    int i ;
    LinkList p = NULL;
    l->next = l ;           /*Circle Link List*/
    for(i=0 ;i<n; i++){
        p = (LinkList)malloc(sizeof(LNode));
        p->data = *(a+i) ;
        p->next = l->next ;
        l->next = p;
    }
}


/*show Link list*/
void ShowLinkListCircle( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p != l){                   /* Here be modifide , because it is circle list*/
        printf("%c,",p->data);
        p = p->next ;
    }
    printf("\n");
}

void ShowLinkList( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p){                   /* Compare with the up function */
        printf("%c,",p->data);
        p = p->next ;
    }
    printf("\n");
}

/*divide the link list*/
void DivideLinkList( LinkList l , LinkList ln , LinkList lc , LinkList lo )
{
    LinkList p , q , r , t ;
    p = l->next ;  /* The first node */
    q = ln ;    /* Number*/
    r = lc ;    /* Charactor*/
    t = lo ;    /* Others*/
    while( p != l ){
        if(p->data>='0' && p->data <= '9'){
            q->next = p ;
            q = p ;
            p = p->next ;
        }else if((p->data>='a' && p->data<='z') || (p->data>='A' && p->data<='Z')){
            r->next = p ;
            r = p ;
            p = p->next ;

        }else{
            t->next = p ;
            t = p ;
            p = p->next ;

        }
    }
    q->next = NULL ;    /*The last one*/
    r->next = NULL ;
    t->next = NULL ;
}

int main(){
    char a1[10] = {'a',' ','Z','1','0','i','"','?','>','3'};
    LinkList lm , la , lb , lc ;
    lm = (LinkList)malloc(sizeof(LNode));
    la = (LinkList)malloc(sizeof(LNode));
    lb = (LinkList)malloc(sizeof(LNode));
    lc = (LinkList)malloc(sizeof(LNode));

    CreateLinkList(lm , a1,10);
    ShowLinkListCircle(lm);
    printf("--------------The Result is --------------\n");
    DivideLinkList(lm,la,lb,lc);
    printf("The number is\n ");
    ShowLinkList(la);
    printf("The charactor is \n");
    ShowLinkList(lb);
    printf("The others is \n");
    ShowLinkList(lc);
    getchar();
}


  6. ( 2.38 练习册 )

/* This program is for 2.38 ( practice )
** Author : kevin
** Date   : 2012/02/18
*/


#include"stdio.h"
#include"alloc.h"
#include"stdlib.h"


typedef struct DouLNode{
    int data ;                  /* stort the data */
    struct DouLNode *next ;     /* The next pointer*/
    struct DouLNode *prior ;    /* The prior pointer*/
    int freq ;                  /* Frequently */
}DouLNode , *LinkList ;

/* create link list */
void CreateLinkList( LinkList l , int *a , int n)
{
    int i ;
    LinkList p = NULL ;
    l->next = l ;
    l->prior = l ;
    for(i=0; i<n; i++){
        p = (LinkList)malloc(sizeof(DouLNode));
        p->data = *(a+i) ;
        p->freq = 0;
        p->next = l->next ;
        p->prior = l ;
        l->next = p ;
        p->next->prior = p;         /*This is very important , double circle list -> Modify */
    }
}

/*show Circle Link list*/
void ShowLinkListCircle( LinkList l)
{
    LinkList p ;
    p = l->next ;
    while(p != l){                   /* Here be modifide , because it is circle list*/
        printf("%d,",p->data);
        printf("%d,",p->freq);
        p = p->next ;
    }
    printf("\n");
}

/* Set the sequence of link list */
void SortAgain( LinkList l , LinkList p)
{
    LinkList q ;
    q = l->next ;
    while( q->freq > p->freq && q != p ){
        q = q->next ;
    }

        if(q!=p)
        {
            p->prior->next = p->next ;
            p->next->prior = p->prior ;

            p->next = q ;
            p->prior = q->prior ;
            q->prior->next = p;
            q->prior = p ;
         }


}

/* Locate the data */
void LocateList( LinkList l , int x )
{
    LinkList r;
    r = l->next ;
    while(r != l ){
        if(r->data == x){
            r->freq++;
            SortAgain(l,r);
            printf("\n%d,%d",r->data,r->freq);
            break;

        }
        r = r->next ;
    }


}


int main()
{
    int data1[10] = {1,2,3,2,3,6,5,8,9,10};
    LinkList lm ;
    lm = (LinkList)malloc(sizeof(DouLNode));
    CreateLinkList(lm , data1 , 10 );
    ShowLinkListCircle(lm);

    printf("-----------The Locate Start-----------\n");
    LocateList(lm , 2);
    LocateList(lm , 3);
    LocateList(lm , 4);
    LocateList(lm , 5);
    LocateList(lm , 2);
    ShowLinkListCircle(lm);

    getchar();

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值