1. 程式人生 > >連結串列(鏈式儲存)的基本操作

連結串列(鏈式儲存)的基本操作

Clist.h

#ifndef CLIST_H_

#define CLIST_H_

typedef struct _tag_CListNode
{
struct _tag_CListNode* next;
}CListNode;

typedef void CList;

CList* CList_Create();

void CList_Destroy(CList* list);

void CList_Clear(CList* list);

int CList_Length(CList* list);

int CList_Insert(CList* list,CListNode* node,int pos);

CListNode* CList_Get(CList* list,int pos);

CListNode* CList_Delete(CList* list,int pos);

#endif

Clist.c

#include "Clist.h"
#include <iostream>


typedef struct _tag_CList
{
CListNode head;
int length;
}TCList;


CList* CList_Create()
{
TCList *ret=NULL;
ret=(TCList *)malloc(sizeof(TCList));
memset(ret,0,sizeof(TCList));


return ret;
}


void CList_Destroy(CList* list)
{
if(list!=NULL)
{
free(list);
list=NULL;
}
return;
}


void CList_Clear(CList* list)
{
TCList *tlist=NULL;
if(list==NULL)
{
return;
}
tlist=(TCList*)list;
tlist->length=0;
tlist->head.next=NULL;
return;
}


int CList_Length(CList* list)
{
TCList *tlist=NULL;
if(list==NULL)
{
return -1;
}
tlist=(TCList*)list;
return tlist->length;
}


int CList_Insert(CList* list,CListNode* node,int pos)
{
int ret=0,i=0;
CListNode *current=NULL;
TCList *tlist=NULL;
if(list==NULL||node==NULL||pos<0)
{
ret=-1;
printf("fun CList_Insert() err:%d",ret);
return ret;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&&(current->next!=NULL);i++)
{
current=current->next;
}
node->next=current->next;
current->next=node;
tlist->length++;
return 0;
}


CListNode* CList_Get(CList* list,int pos)
{
int ret=0,i=0;
CListNode *current=NULL;
TCList *tlist=NULL;
if(list==NULL||pos<0)
{
ret=-1;
printf("fun CList_Get() err:%d",ret);
return NULL;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&&current->next!=NULL;i++)
{
current=current->next;
}


return current->next;
}


CListNode* CList_Delete(CList* list,int pos)
{
int i=0;
CListNode *current=NULL;
CListNode *ret=NULL;


TCList *tlist=NULL;
if(list==NULL||pos<0)
{
printf("fun CList_Get() err:%d",ret);
return NULL;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&&current->next!=NULL;i++)
{
current=current->next;
}
ret=current->next;
current->next=ret->next;
tlist->length--;
return ret;
}

main.c

#include "Clist.h"
#include <iostream>


typedef struct Teacher
{
CListNode node;
int age;
char name[64];
}Teacher;


void main()
{
int len=0,ret=0,i=0;
Teacher t1,t2,t3,t4,t5;
t1.age=29;
t2.age=35;
t3.age=33;
t4.age=34;
t5.age=28;
CList* list=NULL;
list=CList_Create();
if(list==NULL)
{
return;
}


len=CList_Length(list);
ret=CList_Insert(list,(CListNode*)&t1,0);
ret=CList_Insert(list,(CListNode*)&t2,0);
ret=CList_Insert(list,(CListNode*)&t3,0);
ret=CList_Insert(list,(CListNode*)&t4,0);
ret=CList_Insert(list,(CListNode*)&t5,0);


for(i=0;i<CList_Length(list);i++)
{
Teacher *tmp=(Teacher*)CList_Get(list,i);
if(tmp==NULL)
{
return;
}
printf("tmp->age:%d\n",tmp->age);
}


while(CList_Length(list)>0)
{
Teacher *tmp=(Teacher*)CList_Delete(list,0);
if(tmp==NULL)
{
return;
}
printf("Del tmp->age:%d\n",tmp->age);
}


CList_Destroy(list);


system("pause");
return;
}