Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603780
  • 博文数量: 99
  • 博客积分: 5128
  • 博客等级: 大校
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-27 19:40
文章分类

全部博文(99)

文章存档

2012年(3)

2011年(5)

2010年(4)

2009年(31)

2008年(56)

分类: C/C++

2009-07-07 21:45:56

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

#define LIST_MAX_SIZE 10
typedef int elem_t;

typedef struct _List list_t;
struct _List
{
    elem_t data[LIST_MAX_SIZE];
    int tail;
};

// create list

list_t *CreateList(void)
{
    list_t *l;
    l=(list_t *)malloc(sizeof(list_t));
    if (l==NULL)
    {
        printf("could not alloc memory for list!");
        exit(1);
    }
    memset(&l->data,0,LIST_MAX_SIZE*sizeof(elem_t));
    l->tail = 0;
    return l;
}

// delete list

int DeleteList(list_t *l)
{
    free(l);
    return 0;
}

// inser one data into the tail of list

int InserList(list_t *l,int d)
{
    if (l->tail == LIST_MAX_SIZE)
        return 1;

    l->data[l->tail] = d;
    l->tail++;
    return 0;
}

// return one data by address

elem_t ReadTailFromList(list_t *l)
{
    if (l->tail == 0)
    {
        printf("\nError!list is empty");
        exit(1);
    }
    return l->data[l->tail-1];
}

// delete the last data

elem_t DelDataFromTail(list_t *l)
{
    elem_t tmp;
    
    if (l->tail == 0)
    {
        printf("\nError!list is empty");
        exit(1);
    }

    tmp = l->data[l->tail];
    l->tail--;
    return tmp;
}

// whether list is empty

int IsEmptyList(list_t *l)
{
    return !l->tail;
}

int IsFullList(list_t *l)
{
    return (l->tail == LIST_MAX_SIZE);
}

// find data from the list

int FindDataFromList(list_t *l,elem_t d)
{
    int i;
    for (i=0;i<=l->tail;i++)
        if (l->data[i] == d)
            // finded,return the position

            return i;
    // can not find it

    return -1;
}

// inser data at position

int InserDateAtPos(list_t *l,int pos,elem_t d)
{
    int i;
    elem_t t1,t2;

    if (IsFullList(l))
    {
        printf("\nError!list is full");
        exit(1);
    }else if(pos > l->tail){
        printf("\nError!position out of range!");
        exit(1);
    }
    
    l->tail++;

    for(i=pos,t2=d ; i<l->tail ; i++)
    {
        t1 = l->data[i];
        l->data[i] = t2;
        t2 = t1;
    }

    return 0;
}

// delete data at position

elem_t DeleteDateAtPos(list_t *l,int pos)
{
    elem_t retval;

    if (IsEmptyList(l))
    {
        printf("\nError!list is empty");
        exit(1);
    }else if(pos > l->tail){
        printf("\nError!position out of range!");
        exit(1);
    }
    // save data to return

    retval = l->data[pos];

    for( pos ; pos<l->tail ; pos++)
    {
        l->data[pos] = l->data[pos+1];
    }
    
    l->tail--;
    
    return retval;
}

// change data at position

int ChangeDateAtPos(list_t *l,int pos,elem_t d)
{
    if(pos >= l->tail){
        printf("\nError!position out of range!");
        exit(1);
    }
    
    l->data[pos] = d;

    return 0;
}

// traverse list

void TraverseList(list_t *l)
{
    int i=0;

    printf("\n");
    if (l->tail == 0)
    {
        printf("list is empty!");
    }else{
        for(i=0;i<l->tail;i++)
            printf("%d,",l->data[i]);
    }
}

int main(int argc,char *argv[])
{
    list_t *list;
    int i;
    int retval;
    
    list = CreateList();
    
    for (i=0;i<8;i++)
        InserList(list,i*i);
    
    retval = FindDataFromList(list,2);
    printf("\nfind data 2 in list return %d",retval);
    
    TraverseList(list);
    
    InserDateAtPos(list,5,100);

    TraverseList(list);

    DeleteDateAtPos(list,5);

    TraverseList(list);

    DeleteList(list);

    return 0;
}


阅读(603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~