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

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2008-04-18 16:01:04

实现代码:
支持链表的创建、销毁、显现,节点的显现、插入、删除、查找

#include"LNode.h"
LNode *Creat_List(int NodeNum)
{
        LNode *pHead=NULL;
        LNode *p1,*p2;
        p1=p2=(LNode *)malloc(sizeof(LNode));
        if(p1==NULL)
        {
                printf ("Malloc Error!\n");
                return NULL;
        }
        memset(p1,0,sizeof(LNode));
        p1->Data=(char *)malloc(LENGTH_DATA*sizeof(char));
        if(p1->Data==NULL)
        {
                printf ("Malloc Error!\n");
                return NULL;
        }
        pHead=p1;
        int i=0;
        for (;i<NodeNum-1;i++)
        {
                p1=(LNode *)malloc(sizeof(LNode));
                if(p1==NULL)
                {
                        printf ("Malloc Error!\n");
                        return NULL;
                }
                memset(p1,0,sizeof(LNode));
                p1->Data=(char *)malloc(LENGTH_DATA*sizeof(char));
                if(p1->Data==NULL)
                {
                        printf ("Malloc Error!\n");
                        return NULL;
                }
                p2->next=p1;
                p2=p1;
        }
        p1->next=NULL;
        return pHead;
}
Status Show_List(LNode *L,char *Message)
{
        if(L==NULL)
        {
                return ERROR;
        }
        LNode *pL=L;
        int i=1;
        printf("-------------------After %s---Show List-----------------------\n",Message);
        while(pL)
        {
                printf("[%d]Data:%s\n",i,pL->Data);
                pL=pL->next;
                i++;
        }
        printf("-----------------------------------------------------------\n");
        return OK;
}
Status Free_List(LNode *L)
{
        if(L==NULL)
        {
                return ERROR;
        }
        LNode *pHead=L;
        LNode *pNext=pHead->next;
        while(pHead->next!=NULL)
        {
                pHead->next=pNext->next;
                if(pNext->Data!=NULL){
                        free(pNext->Data);
                        pNext->Data=NULL;
                }
                if(pNext!=NULL){
                        free(pNext);
                }
                pNext=pHead->next;
        }
        if(pHead->next==NULL)
        {
                if(pHead->Data!=NULL){
                        free(pHead->Data);
                        pHead->Data=NULL;
                }
                if(pHead!=NULL){
                        free(pHead);
                        pHead=NULL;
                }
        }
        return OK;
}//General Method

void Destroy_List(LNode*L)
{
        if(L!=NULL)
        {
                Destroy_List(L->next);
                if(L->Data!=NULL){
                        free(L->Data);
                }
                if(L){
                        free(L);
                }
                L=NULL;
        }
}// Recursion

Status Equal_Node(LNode *L,int iPos,const char *src,int iDatalen)
{
        //iDatalen is the length of L->Data

        if(L==NULL||iPos<1)
        {
                return ERROR;
        }
        int i=1;
        LNode *pL=L;
        while(pL&&i<iPos)
        {
                pL=pL->next;
                i++;
        }
        if(L==NULL&&i<iPos)
        {
                return OVERFLOW;
        }
        int length=(iDatalen>=strlen(src)?strlen(src):iDatalen);
        strncpy(pL->Data,src,length);
        return OK;
}
Status GetData_Node(LNode *L,int iPos,char *dst)
{
        if(L==NULL||iPos<1)
        {
                return ERROR;
        }
        LNode *pL=L;
        int i=1;
        while(pL!=NULL&&i<iPos)
        {
                pL=pL->next;
                i++;
        }
        if(L==NULL&&i<iPos)
        {
                return OVERFLOW;
        }
        strcpy(dst,pL->Data);
        return OK;
}
Status Insert_Node(LNode **L,int iPos,const char * str,int len)
{
        if(L==NULL||iPos<1)
        {
                return ERROR;
        }
        LNode *pL=*L;
        LNode *pNew=NULL;
        LNode *pNext=NULL;
        pNew=(LNode *)malloc(sizeof(LNode));
        if(pNew==NULL)
        {
                printf("Creat NewNode Error!");
                return OVERFLOW;
        }
        pNew->Data=(char *)malloc(sizeof(char)*LENGTH_DATA);
        if(pNew->Data==NULL)
        {
                printf("Creat NewNode_Data Error!");
                return OVERFLOW;
        }
        int length=(len>LENGTH_DATA?LENGTH_DATA:len);
        strncpy(pNew->Data,str,length);
        int i=1;
        if(iPos=1)
        {
                *L=pNew;
                pNew->next=pL;
        }
        else
        {
                while(pL&&i<iPos-1)
                {
                        pL=pL->next;
                        i++;
                }//Get the previous Node of pointed Node;

                if(pL==NULL&&i<iPos-1)
                {
                        return ERROR;
                }
                if(pL->next==NULL)
                {
                        return ERROR;
                }
                pNext=pL->next;
                pL->next=pNew;
                pNew->next=pNext;
        }
        return OK;
}//Insert NewNode before pointed_node

Status Delete_Node(LNode **L,int iPos)
{
        if(L==NULL||iPos<1)
        {
                return ERROR;
        }
        int i=1;
        LNode *pL=*L;
        LNode *pNext,*pPre;
        if(iPos==1)
        {
                *L=pL->next;
                if(pL){
                        free(pL->Data);
                        free(pL);
                }
        }
        else
        {
                while(pL&&i<iPos)
                {
                        pPre=pL;
                        pL=pL->next;
                        if(pL)
                        {
                                pNext=pL->next;
                        }
                        else
                        {
                                pNext=NULL;
                        }
                        i++;
                }
                if(pL==NULL&&i<iPos)
                {
                        return ERROR;
                }
                pPre->next=pNext;
                if(pL){
                        free(pL->Data);

                        free(pL);
                }
        }
        return OK;
}//Delete the pointed node

Status LocateData_Node(LNode *L,const char *str)
{
        if (!L || !str)
        {
                return ERROR;
        }
        LNode *pL=L;
        while(pL)
        {
                if(0==strncmp(pL->Data,str,strlen(str)))
                {
                        return OK;
                }
                pL=pL->next;
        }
        return ERROR;
}//Find Data

        int
main(void)
{
        LNode *pList=NULL;
        pList=Creat_List(5);
        char ShowBuf[128]={0};
        if(pList!=NULL)
        {
                printf("Creat_List Success!\n");
        }
        Equal_Node(pList,1,"FirstNode",LENGTH_DATA);
        GetData_Node(pList,1,ShowBuf);
        printf("Data:%s\n",ShowBuf);
        Equal_Node(pList,2,"SecondNode",LENGTH_DATA);
        Equal_Node(pList,3,"ThirdNode",LENGTH_DATA);
        Equal_Node(pList,4,"ForthNode",LENGTH_DATA);
        Equal_Node(pList,5,"FifthNode",LENGTH_DATA);
        Show_List(pList,"Equal");
        Insert_Node(&pList,1,"NewFifthNode",strlen("NewForthNode"));
        Show_List(pList,"Insert");
        Delete_Node(&pList,1);
        Show_List(pList,"Delete");
        if(OK==LocateData_Node(pList,"ThirdNode"))
        {
                printf("find Data!\n");
        }
#if 0
        if(Free_List(pList)==OK)
        {
                printf("Free_List Success!\n");
        }
        return 0;
#else
        Destroy_List(pList);
#endif
}

头文件:

#ifndef _LNODE_H
#define _LNODE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "type.h"
#define LENGTH_DATA 64

typedef struct LNode
{
        char *Data;
        struct LNode *next;
}LNode;

#endif

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