欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言順序表實(shí)現(xiàn)代碼排錯(cuò)

 更新時(shí)間:2013年12月08日 11:21:45   作者:  
這篇文章主要介紹了C語(yǔ)言順序表實(shí)現(xiàn)方法,大家參考使用吧

今天本來(lái)想寫段代碼練練手,想法挺好結(jié)果,栽了個(gè)大跟頭,在這個(gè)錯(cuò)誤上徘徊了4個(gè)小時(shí)才解決,現(xiàn)在分享出來(lái),給大家提個(gè)醒,先貼上代碼:

復(fù)制代碼 代碼如下:

/********************************************
 * 文件名稱:sqlist.h
 * 文件描述:線性表順序存儲(chǔ)演示
 * 文件作者:by Wang.J,in 2013.11.16
 * 文件版本:1.0
 * 修改記錄:
*********************************************/
#ifndef __SQLIST_H__
#define __DWLIST_H__

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

#define MAXSIZE     50
#define OK          0
#define ERR         -1

typedef int elemtype;

typedef struct {
    elemtype data[MAXSIZE];
    int      len;
}sqlist;

int init_list(sqlist *L);
int destroy_list(sqlist *L);
int list_empty(sqlist L);
int list_length(sqlist L);
int disp_list(sqlist L);
int get_elem(sqlist L, int i, elemtype *e);
int local_elem(sqlist L, elemtype e);
int list_insert(sqlist *L, int i, elemtype e);
int list_delete(sqlist *L, int i, elemtype *e);

#endif


/**************************************************
 * 文件名稱:sqlist.c
 * 文件描述:線性表順序存儲(chǔ)的實(shí)現(xiàn)
 * 文件作者:by Wang.J,in 2013.11.16
 * 文件版本:1.0
 * 修改記錄:
***************************************************/
#include "sqlist.h"

#if 0
#define ERR_NONE_ERROR        0
#define ERR_FUNC_EXEC         1
#define ERR_FILE_OPEN         2

char *error_msg[] = {
    /* 0  */    "成功執(zhí)行,無(wú)錯(cuò)誤",
    /* 1  */    "函數(shù)執(zhí)行錯(cuò)誤",
    /* 2  */    "文件打開錯(cuò)誤",
};
int my_errno = 0;
#endif

int main(void)
{
    int ret = 0;
    int i = 0;
    sqlist slist;
    elemtype e;

    memset(&slist, 0, sizeof(slist));
    printf("length:%d\n", slist.len);
    ret = init_list(&slist);
    if (OK != ret)
        return -1;

    ret = list_empty(slist);
    printf("長(zhǎng)度:%d\n", slist.len);
    if (OK == ret)
        printf("順序表為空\(chéng)n");
    if (ERR == ret)
        printf("順序表不為空\(chéng)n");

    for (i = 0; i < 10; i++) {
        e = (elemtype)i;
        list_insert(&slist, i, e);
    }
    printf("插入數(shù)據(jù)\n");

    ret = list_empty(slist);
    if (OK == ret)
        printf("順序表為空\(chéng)n");
    if (ERR == ret)
        printf("順序表不為空\(chéng)n");

    printf("after length%d\n", list_length(slist));

    disp_list(slist);

    destroy_list(&slist);

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:init_list
 * 函數(shù)功能:初始化一個(gè)順序表,創(chuàng)建一個(gè)空的順序表
 * 函數(shù)參數(shù):sqlist *L   負(fù)責(zé)返回一個(gè)創(chuàng)建好的順序表,如果創(chuàng)建
            失敗則返回NULL
 * 返 回 值:成功返回0并通過指針返回一個(gè)創(chuàng)建好的空表
            失敗返回-1指針返回NULL
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int init_list(sqlist *L)
{
    L = (sqlist *)malloc(sizeof(sqlist));

    if (NULL == L) {
        L = NULL;
        return -1;
    }

    L->len = 0;

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:destroy_list
 * 函數(shù)功能:銷毀創(chuàng)建好的順序表,釋放順序表的空間
 * 函數(shù)參數(shù):sqlist *L,已經(jīng)存在的線性表
 * 返 回 值:成功     0
            失敗     -1
            通常free不會(huì)失敗,其實(shí)這個(gè)函數(shù)可以直接使用void
            的,這里只是自己順手寫的,看到代碼就知道不會(huì)返回0
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int destroy_list(sqlist *L)
{
    free(L);

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:list_empty
 * 函數(shù)功能:判斷sqlist順序表是否為空
 * 函數(shù)參數(shù):sqlist L,已存在的線性表
 * 返 回 值:空     0
            不空   -1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int list_empty(sqlist L)
{
    if (0 == L.len)
        return 0;

    return -1;
}

/*=====================================================
 * 函數(shù)名稱:list_length
 * 函數(shù)功能:取得線性表的長(zhǎng)度,返回順序表中元素個(gè)數(shù)
 * 函數(shù)參數(shù):sqlist L,已經(jīng)存在的線性表
 * 返 回 值:L的長(zhǎng)度
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int list_length(sqlist L)
{
    return L.len;
}

/*=====================================================
 * 函數(shù)名稱:disp_list
 * 函數(shù)功能:顯示順序表中所有的元素
 * 函數(shù)參數(shù):sqlist L,已經(jīng)存在的線性表
 * 返 回 值:成功     0
            失敗     -1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int disp_list(sqlist L)
{
    int i = 0;

    if (0 >= L.len)
        return -1;

    for (i = 0; i < L.len; i++)
        printf("%d\t", L.data[i]);
    /*
     * 這個(gè)地方我自己是有異議的,首先你可能不知道輸出的類型為
     * %d,再就是求長(zhǎng)度是使用list_length函數(shù)還是使用L.len方式,
     * list_length是函數(shù)調(diào)用有著函數(shù)調(diào)用的額外開銷,在PC上這點(diǎn)
     * 開銷不算什么,但是在嵌入式系統(tǒng)就不得不考慮這種開銷了,
     * 這基本上算是良好的移植性和代碼效率之間的問題,為了提高
     * 移植性可以多添加幾層抽象層,實(shí)現(xiàn)各種判斷.除非是極其龐大
     * 的項(xiàng)目或是為了匹配各種這樣的設(shè)備,我認(rèn)為像代碼定義類型這
     * 種小事,團(tuán)隊(duì)溝通就能解決.工作是避免問題,學(xué)習(xí)是自找問題.
     * 所以怎么取舍只能看個(gè)人了.
    */
    printf("\n");

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:get_elem
 * 函數(shù)功能:獲取i位置元素的值域,為了方便對(duì)應(yīng)i從0開始與
            數(shù)組下標(biāo)一致,用e返回獲取的值
 * 函數(shù)參數(shù):sqlite L    存在的順序表
            int    i    位置
            elemtype *e 返回值域
 * 返 回 值:成功     0
            失敗     -1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int get_elem(sqlist L, int i, elemtype *e)
{
    if (i < 0 || i >= L.len) {
        e = NULL;
        return -1;
    }

    *e = L.data[i];
    /*
     * 這個(gè)地方要注意
     * 看看與e = &(L.data[i])區(qū)別
    */

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:local_elem
 * 函數(shù)功能:按元素值查找,返回第一個(gè)與e相匹配的元素位置
 * 函數(shù)參數(shù):sqlist L,已經(jīng)存在的順序表
 * 返 回 值:存在返回位置
            失敗返回-1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int local_elem(sqlist L, elemtype e)
{
    int i = 0;

    for (i = 0; i < L.len; i++) {
        if (e == L.data[i])
            return i;
    }

    return -1;
}

/*=====================================================
 * 函數(shù)名稱:list_insert
 * 函數(shù)功能:在sqlite的i位置插入元素
 * 函數(shù)參數(shù):sqlist *L   已存在的順序表
            int     i   位置
            elemtype e  元素
 * 返 回 值:成功   0
            失敗   -1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int list_insert(sqlist *L, int i, elemtype e)
{
    int j = 0;

    if (i < 0 || i > MAXSIZE-1)
        return -1;

    for (j = L->len; j > i; j--)
        L->data[j] = L->data[j-1];

    L->data[i] = e;
    L->len++;

    return 0;
}

/*=====================================================
 * 函數(shù)名稱:list_delete
 * 函數(shù)功能:刪除i位置的元素,元素通過e返回
 * 函數(shù)參數(shù):sqlite  *L  已存在的順序表
            int      i  位置
            elemtype *e 刪除位置的元素
 * 返 回 值:成功    0
            失敗    -1
 * 創(chuàng) 建 人:by Wang.J,in 2013.11.16
 * 修改記錄:
======================================================*/
int list_delete(sqlist *L, int i, elemtype *e)
{
    int j = 0;

    if (i < 0 || i >=L->len)
        return -1;

    *e = L->data[i];
    for (j = i; j < (L->len-1); j++)
        L->data[j] = L->data[j+1];

    L->len--;

    return 0;
}

很自得,自認(rèn)為寫的很好,運(yùn)行一下看看,

結(jié)果完全出乎意料.

好吧!現(xiàn)在分析錯(cuò)誤!

看看main中的定義

復(fù)制代碼 代碼如下:

int ret = 0;
   int i = 0;
   sqlist slist;
   elemtype e;


看看初始化函數(shù)init_list

復(fù)制代碼 代碼如下:

int init_list(sqlist *L)
{
   L = (sqlist *)malloc(sizeof(sqlist));

    if (NULL == L) {
        L = NULL;
        return -1;
    }

    L->len = 0;

    return 0;
}

相信聰明的你已經(jīng)看出來(lái)了,我在main中定義的slist空間在棧上,而我在init_list中一下子將這個(gè)東東分配到了堆空間,并且slist并不是指針,根本無(wú)法進(jìn)行指向,所以結(jié)果當(dāng)然就非常的錯(cuò)誤了.

打個(gè)比方,棧和堆是兩個(gè)平行的世界,只有指針是穿梭于兩個(gè)世界的蟲洞,除此以為其他東西無(wú)法進(jìn)行跨越.

知道了原因自然很容易解決了.

由于棧上會(huì)自動(dòng)分配空間所以就無(wú)需再次申請(qǐng)空間.所以init_list改為:

復(fù)制代碼 代碼如下:

int init_list(sqlist *L)
{
    /*
    L = (sqlist *)malloc(sizeof(sqlist));

    if (NULL == L) {
        L = NULL;
        return -1;
    }
    */
    L->len = 0;

    return 0;
}

就可以了

大家引以為戒.

相關(guān)文章

最新評(píng)論