码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构——广义表

时间:2015-12-01 23:10:37      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

定义

Python中的list就是一种广义表,使用起来非常方便。广义表的特点就是能够存储不同类型的元素,也支持嵌套使用,即表中还有表。关于广义表的定义还有几本操作归纳如下:

ADT Linear List:

元素组成:
能存储不同元素类型的表结构叫做广义表,支持嵌套操作。

基本操作:
InitGList() 创建一个空表
DestoryGList() 销毁一个表
GListLength()  获取表的长度
GListDepth()  获取表的深度
PrintList() 遍历一次表
InsertList()  插入操作
DeleteList()  删除操作

存储结构

广义表的特点使得其使用链式结构来实现较方便,那用什么样的结构来表示广义表中的元素呢?它必须同时支持普通元素也能够表示表结构。需要使用一个标志符来说明是普通元素还是表,如果是普通元素,则接下来存储它的值,如果是表结构,则存储指向该表的指针。最后再是一个指向下一个元素的指针。其定义如下:

typedef enum
{ATOM, LIST}ElemTag;

typedef struct GNode
{
    ElemTag tag;
    union
    {
        int data;
        struct GNode *head;
    };

    struct GNode *next;
}GList;

 

下面通过几个例子的示意图来更好的理解。

A = [] // A是一个空表

B = [1, 2] // B存储两个普通元素

C = [3, B, 4] // C既有普通元素也有表

技术分享

实现

下面是用C语言来实现的广义表定义还有一些基本的操作。

/*
 *
 *
 * this file is to define and release GList struct 
 *
 *
 *
 *
 */


// GList node

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

typedef enum
{ATOM, LIST}ElemTag;

typedef struct GNode
{
    ElemTag tag;
    union
    {
        int data;
        struct GNode *head;
    };

    struct GNode *next;
}GList;



GList *InitGList()
{
    GList *phead = NULL;
    if ((phead = (GList*)malloc(sizeof(GList))) == NULL)
    {
        printf("malloc failed\n");
        return NULL;
    }

    phead->tag = LIST;
    phead->head = NULL;
    phead->next = NULL;
    return phead;
}

int GetGListLength(GList *phead)
{
    int len = 0;
    if (phead == NULL || phead->head == NULL) 
        return len;

    phead = phead->head;
    while (phead != NULL)
    {
        len += 1;
        phead = phead->next;
    }

    return len;
}


int GetGListDepth(GList *phead)
{
    int depth = 0;
    int tmp = 0;
    if (phead == NULL || phead->head == NULL) 
        return depth;

    phead = phead->head;
    while (phead != NULL)
    {
        if (phead->tag == ATOM)
            tmp = 1;
        else
            tmp = GetGListDepth(phead) + 1;

        depth = tmp > depth ? tmp : depth;
        
        phead = phead->next;
    }
    return depth;
}


void PrintGList(GList *phead)
{
    if (phead == NULL || phead->head == NULL) 
        return;

    phead = phead->head;
    while (phead != NULL)
    {
        if (phead->tag == ATOM)
            printf("%d\n", phead->data);
        else
            PrintGList(phead);
        phead = phead->next;
    }
}


GList *InsertGList(GList *phead, GList *pinsert, int position)
{
    if (phead == NULL || phead->head == NULL)
        return;
    
    int len = GetGListLength(phead);
    if (position < 1 || position > len + 1)
    {
        printf("the position is not right\n");
        return;
    }

    GList *ptmp;
    if (position == 1)
    {
        ptmp = phead->head; 
        phead->head = pinsert;
        pinsert->next = ptmp;
        return phead;
    }
    
    phead = phead->head;
    int i;
    for (i = 1; i < position - 1; i++)
        phead = phead->next;

    ptmp = phead->next;
    phead->next = pinsert;
    pinsert->next = ptmp;
    return phead;
}



GList *DeleteGList(GList *phead, int position)
{
     if (phead == NULL || phead->head == NULL)
        return;
    
    int len = GetGListLength(phead);
    if (position < 1 || position > len)
    {
        printf("the position is not right\n");
        return;
    }
    
    GList *ptmp;
    if (position == 1)
    {
        ptmp = phead->head;
        phead->head = ptmp->next;
        free(ptmp);
        return phead;
    }

    phead = phead->head;
    int i;
    for (i = 1; i < position - 1; i++)
        phead = phead->next; 

    ptmp = phead->next;
    phead->next = ptmp->next;
    free(ptmp);
    return phead;
}

数据结构——广义表

标签:

原文地址:http://www.cnblogs.com/Gru--/p/5011358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!