Chinaunix首页 | 论坛 | 博客
  • 博客访问: 344741
  • 博文数量: 82
  • 博客积分: 2602
  • 博客等级: 少校
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-10 08:48
文章分类

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2008-04-17 15:14:34


结合严慧敏《数据结构(c语言版)》



实现代码:
包含了初始化、赋值、插入元素、删除指定位置元素、两表合并、两表取并集等操作,适应于char型

#include "LineTable.h"

Status ShowList_Sq(SqList *L,char *Message)
{
        if(L->elem==NULL)
        {
                return ERROR;
        }
        printf("-------------------%s Start------------------------:\n",Message);
        printf("\tData:%s\n",L->elem);
        printf("\tLen:%d\n",L->length);
        printf("\tSize:%d\n",L->listsize);
        printf("-------------------%s End------------------------:\n",Message);
        return TRUE;
}//Show


Status InitList_Sq(SqList **L)
{
        (*L)->elem=(char *)malloc(LIST_INIT_SIZE*sizeof(char));
        if((*L)->elem==NULL)
        {
                return OVERFLOW;
        }
        (*L)->length=0;
        (*L)->listsize=LIST_INIT_SIZE;
        memset((*L)->elem,0,(*L)->listsize);
        printf("Init OK!\n");
        return OK;
}//Init

Status EqualList_Sq(SqList *L,char *s)
{
        int len=strlen(s);
        if(len > L->listsize-1)
        {
                return ERROR;
        }
        strncpy(L->elem,s,len);
        L->length=len+1;
        return OK;
}//Equal


Status InsertList_Sq(SqList **L,int pos, char e)
{
        if(pos<1||pos>(*L)->length+1)
        {
                return ERROR;
        }

        if((*L)->length>=(*L)->listsize)
        {

                printf("Need Realloc!\n");
                (*L)->elem=(char*)realloc((*L)->elem,((*L)->listsize+LISTINCREMENT)*sizeof(char));
                if((*L)->elem==NULL)
                {
                        return OVERFLOW;
                }
        }
        char *pPos=&((*L)->elem[pos-1]);
        char *pEnd=&((*L)->elem[(*L)->length-1]);
        for(;pEnd>=pPos;pEnd--)
                *(pEnd+1)=*pEnd;
        *pPos=e;
        ++(*L)->length;
        (*L)->listsize+=LISTINCREMENT;
        return OK;
}//Insert

Status DeletList_Sq(SqList *L,int pos,char *e)
{
        if(pos<1||pos>L->length+1)
        {
                return ERROR;
        }
        char *p=&(L->elem[pos-1]);
        char *q=&(L->elem[L->length-1]);
        *e=*p;
        for(;p<=q;p++)
                *p=*(p+1);
        L->length--;

}//Delete

Status FindElem_Sq(SqList *L,char e)
{
        if(L->elem==NULL)
        {
                return ERROR;
        }
        char *p=L->elem;
        while(*p++)
        {
                if(*p==e)
                {
                        return OK;
                }
        }
        return FALSE;
}//FindElem

Status Append_Sq(SqList **L,SqList *p)
{
        if((*L)->elem==NULL||p->elem==NULL)
        {
                return ERROR;
        }
        if((*L)->length + p->length>(*L)->listsize)
        {
                printf("Memory is not enough!\n");
                (*L)->elem=(char *)realloc((*L)->elem,(((*L)->length + p->length -1 +LISTINCREMENT)*sizeof(char)));
        }
        char *pL=(*L)->elem;
        char *pp=p->elem;
        while(*pL++);
        pL--;
        while(*pL++=*pp++);
        (*L)->length=(*L)->length + p->length-1;
        (*L)->listsize=(*L)->length+LISTINCREMENT-1;
        return OK;
}//Union

Status Union_Sq(SqList **L,SqList*p)
{
        if((*L)->elem==NULL||p->elem==NULL)
        {
                return ERROR;
        }
        if((*L)->length + p->length>(*L)->listsize)
        {
                printf("Memory is not enough!\n");
                (*L)->elem=(char *)realloc((*L)->elem,(((*L)->length + p->length -1 +LISTINCREMENT)*sizeof(char)));
        }
        int i=0;
        char *pp=p->elem;
        for(;i<p->length-1;i++,pp++)
        {
                if(FALSE==FindElem_Sq(*L,*pp))
                {
                        printf("Insert[%c]\n",*pp);
                        InsertList_Sq(L,(*L)->length,*pp);
                }
        }
}//Union

int
main(void)
{
        SqList Array,*pArray=&Array;
        SqList Src,*pSrc=&Src;
        char c;
        char e='3';
        if((InitList_Sq(&pArray)!=OK)||(InitList_Sq(&pSrc)!=OK))
        {
                printf("Init Error!\n");
                return ERROR;
        }
        EqualList_Sq(pArray,"a234bcdefgh");
        EqualList_Sq(pSrc,"tomwht");
        ShowList_Sq(pArray,"Init");
        InsertList_Sq(&pArray,4, e);
        ShowList_Sq(pArray,"Insert");
        DeletList_Sq(pArray,5,&c);
        ShowList_Sq(pArray,"Delete");
        if(TRUE==FindElem_Sq(pArray,e))
        {
                printf("[%c] is found!\n",e);
        }
        Append_Sq(&pArray,pSrc);
        ShowList_Sq(pArray,"Append");
// Union_Sq(&pArray,pSrc);

// ShowList_Sq(pArray,"Union");

        free(pArray->elem);
        pArray->elem=NULL;
        free(pSrc->elem);
        pSrc->elem=NULL;
        return OK;




头文件:

#ifndef _LINETABLE_H
#define _LINETABLE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "type.h"

#define LIST_INIT_SIZE 20
#define LISTINCREMENT 10

typedef struct LineArray
{
        char *elem;
        int length;
        int listsize;
}SqList;
#endif

#ifndef _TYPE_H
#define _TYPE_H

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType ;

#endif

阅读(2738) | 评论(0) | 转发(0) |
0

上一篇:vi编辑器的快捷键

下一篇:单链表的c实现

给主人留下些什么吧!~~