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

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2008-04-22 16:17:34

#include "LinkList.h"
LNode * MakeNode(const char *value)
{
        int len=0;
        LNode *pNode=(LNode *)malloc(sizeof(LNode));
        if(!pNode)
        {
                return NULL;
        }
        pNode->Data=(char *)malloc(sizeof(char)*DATA_LEN);
        if(!pNode->Data)
        {
                return NULL;
        }
        len=(DATA_LEN-1>strlen(value)?strlen(value):(DATA_LEN-1));
        strncpy(pNode->Data,value,len);
        return pNode;
}//构造节点

Status FreeNode(LNode *pNode)
{
        if(!pNode)
        {
                return ERROR;
        }
        if(pNode->Data)
        {
                free(pNode->Data);
                pNode->Data=NULL;
        }
        free(pNode);
        pNode=NULL;
        return OK;
}//释放节点

LinkList *InitList(int Num,char **value)
{
        if(Num<2){
                return ERROR;
        }
        LinkList *L=(LinkList *)malloc(sizeof(LinkList));
        if(!L)
        {
                return ERROR;
        }
        L->head=MakeNode(value[0]);
        L->tail=MakeNode(value[Num-1]);
        L->head->next=L->tail;
        L->tail->next=NULL;
        L->len=2;
        LNode *p1,*p2;
        int i=Num-2;
        if(Num>2)
        {
                p2=p1=MakeNode(value[i]);
                L->head->next=p1;
                p1->next=L->tail;
                L->tail->next=NULL;
                L->len++;
                i--;
                for (;Num-1>2;Num--,i--)
                {
                        p2=MakeNode(value[i]);
                        L->head->next=p2;
                        p2->next=p1;
                        p1=p2;
                        L->len++;
                }
        }
        return L;
}//构造指定数目节点的链表

void ShowList(LinkList *L)
{
        if(!L)
        {
                return;
        }
        LNode *p=L->head;
        printf("__________________ShowListData:[%d]___________\n",L->len);
        int i=1;
        while(p)
        {
                printf("[%d]Node:%s\n",i,p->Data);
                p=p->next;
                i++;
        }
        printf("_________________________________________\n");
}//显示节点数据

Status DestroyList(LinkList *L)
{
        if(!L)
        {
                return ERROR;
        }
        if(!L->head||!L->tail)
        {
                free(L);
                L=NULL;
        }
        else
        {
                LNode *p=L->head;
                LNode *pNext=p->next;
                do
                {
                        printf("Free Node!\n");
                        free(p->Data);
                        p->Data=NULL;
                        free(p);
                        p=NULL;
                        if(pNext)
                        {
                                p=pNext;
                                pNext=pNext->next;
                        }
                        L->len--;
                }while(p);
                L->tail=NULL;
                L->head=NULL;
                free(L);
                L=NULL;
        }
        return OK;
}//销毁链表

Status ClearList(LinkList *L)
{
        if(!L||!L->head)
        {
                return ERROR;
        }
        LNode *p=L->head;
        LNode *pNext=p->next;
        do
        {
                free(p->Data);
                p->Data=NULL;
                free(p);
                p=NULL;
                if(pNext)
                {
                        p=pNext;
                        pNext=pNext->next;
                }
                L->len--;
        }while(p);
        L->head=NULL;
        L->tail=NULL;
        return OK;
}//置链表为空表


Status InsFirst(LinkList *L,LNode *pNode)
{
        if(!L)
        {
                return ERROR;
        }
        if(!L->head||!pNode)
        {
                return ERROR;
        }
        pNode->next=L->head;
        L->head=pNode;
        L->len++;
        return OK;
}//增加一个结点并作为头结点

Status DelFirst(LinkList *L,LNode **pNode)
{
        if(!L||!L->head)
        {
                return ERROR;
        }
        LNode *p=L->head;
        L->head=L->head->next;
        *pNode=p;
        (*pNode)->next=NULL;
        L->len--;
        return OK;
}//删除头结点

Status Append(LinkList *L,LNode *pNew)
{
        if(!L||!pNew)
        {
                return ERROR;
        }
        L->tail->next=pNew;
        L->tail=pNew;
        pNew->next=NULL;
        L->len++;
        return OK;
}//附加新结点在链表尾

Status Remove(LinkList *L,LNode **pNode)
{
        if(!L||!L->head)
        {
                return ERROR;
        }
        LNode *p=L->head;
        do
        {
                p=p->next;
        }while(p->next->next);
        p->next=NULL;
        *pNode=L->tail;
        (*pNode)->next=NULL;
        L->tail=p;
        L->len--;
        return OK;
}
        int
main(void)
{
        char *ValueBuf[5]={"FirstNode","SecondNode","ThirdNode","FourthNode","FifthNode"};
        LinkList *List=InitList(5,ValueBuf);
        if(List)
        {
                printf("Creat_List Ok!\n");
        }
        ShowList(List);
#if 0
        ClearList(List);
        ShowList(List);
#endif

        LNode *pNode=MakeNode("InsertFirst");
        if(OK==InsFirst(List,pNode))
        {
                printf("InsFirst OK!\n");
                ShowList(List);
        }
        LNode *NewNode =NULL;
        if(OK==DelFirst(List,&NewNode))
        {
                printf("DelFirst OK!\n");
                ShowList(List);
                FreeNode(NewNode);
        }

        LNode *pApp=MakeNode("AppendFirst");
        if(pApp)
        {
                printf("Append OK!\n");
                Append(List,pApp);
                ShowList(List);
        }
#if 1
        LNode *pRmv=NULL;
        if(OK==Remove(List,&pRmv))
        {
                printf("Remove OK!\n");
                printf("Remove:%s\n",pRmv->Data);
                ShowList(List);
                FreeNode(pRmv);
        }
#endif
        if(OK==DestroyList(List))
        {
                printf("Free_List Ok!\n");
        }
        return 0;
}

头文件:

#ifndef _LINK_LIST_H
#define _LINK_LIST_H
#include "type.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_LEN 64

typedef struct LNode
{
        char * Data;
        struct LNode *next;
}LNode;
typedef struct LinkList
{
        LNode *head,*tail;
        int len;
}LinkList;

#endif

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

上一篇:单链表的c实现

下一篇:顺序字母栈的实现

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