1.链式线性表头文件
#ifndef _LINK_LIST_H #define _LINK_LIST_H //定义一个节点 typedef struct _tag_LinkListNode { _tag_LinkListNode *next; }LinkListNode; //定义一个链表@1链表包含一个头部节点@ 包含链表的长度 typedef struct _LinkList { LinkListNode Head; int nLen;//长度 }LinkList; LinkList *linkList_Create(); int linkList_Insert(LinkList *pList,int nPos,LinkListNode *pNode); int linkList_Length(LinkList* pList); LinkListNode* linkList_Get(LinkList* pList,int nPos); LinkListNode* linkList_Del(LinkList* pList,int nPos); void linkList_Destroy(LinkList *pList); void linkList_Clear(LinkList* pList); #endif //_LINK_LIST_H
2.链式线性表源文件
#include "LinkList.h" #include <iostream> using namespace std; LinkList * linkList_Create() { LinkList *pList=new LinkList; if (pList==NULL) { cout<<" linkList_Create() is error "<<endl; return NULL; } memset(pList,0,sizeof(LinkList)); pList->nLen=0; pList->Head.next=NULL; return pList; } int linkList_Insert(LinkList *pList,int nPos,LinkListNode *pNode) { if (pList==NULL||nPos<0||pNode==NULL) { cout<<" linkList_Insert() parameter is error "<<endl; return -1; } //插入一个元素 //定义一个头节点 LinkListNode *current=NULL; current=(LinkListNode*)&pList->Head; for (int i=0;i<nPos;i++) { current=current->next; } //工两步操作 pNode->next=current->next;//currents->next赋值为pNote->next current->next=pNode;//将pNode节点赋值给currentNext节点 pList->nLen++; return 0; } int linkList_Length(LinkList* pList) { if (pList==NULL) { cout<<" linkList_Length() parameter is error "<<endl; return -1; } return pList->nLen; return 0; } LinkListNode* linkList_Get(LinkList* pList,int nPos) { if (pList==NULL||nPos<0||nPos>linkList_Length(pList)) { cout<<" linkList_Get() parameter is error "<<endl; return NULL; } LinkListNode *pCurrent=NULL; //pCurrent=(LinkListNode*)&pList->Head; pCurrent=(LinkListNode*)&pList->Head.next; for (int i=0;i<nPos;i++) { pCurrent=pCurrent->next; } return pCurrent->next; /*下面是测试用 // LinkListNode *pCurrent1=NULL; // LinkListNode *pCurrent2=NULL; // pCurrent1=(LinkListNode*)&pList->Head; // pCurrent2=(LinkListNode*)&pList->Head.next; // // cout<<"pCurrent1"<<pCurrent1<<endl; // cout<<"pCurren2"<<pCurrent2<<endl; */ } LinkListNode* linkList_Del(LinkList* pList,int nPos) { if (pList==NULL||nPos<0||nPos>linkList_Length(pList)) { cout<<" linkList_Del() parameter is error "<<endl; return NULL; } LinkListNode *pCurrent=NULL; pCurrent=(LinkListNode*)&(pList->Head); //LinkListNode* pNode=linkList_Get(pList,nPos); for (int i=0;i<nPos;i++) { pCurrent=pCurrent->next; } LinkListNode* pDelNode=pCurrent->next; pList->nLen--; return pDelNode; } void linkList_Destroy(LinkList *pList) { if (pList==NULL) { cout<<" LinkList_Destroy() parameter is error "<<endl; return; } if (pList!=NULL) { delete pList; } pList=NULL; } void linkList_Clear(LinkList* pList) { if (pList==NULL) { cout<<" linkList_Clear() parameter is error "<<endl; return; } pList->Head.next=NULL; pList->nLen=0; }
3.链式线性表 测试框架
#include <iostream> #include "LinkList.h" using namespace std; typedef struct _Teacher { LinkListNode node; char name[64]; int nAge; }Teacher; int main() { LinkList *pList=linkList_Create(); if (pList==NULL) { cout<<"create LinkList Fail"<<endl; return -1; } Teacher t1,t2,t3; t1.nAge=10; strcpy(t1.name,"小李"); t2.nAge=11; strcpy(t2.name,"小张"); t3.nAge=12; strcpy(t3.name,"小刘"); int nRect=linkList_Insert(pList,0,(LinkListNode*)&t1); nRect=linkList_Insert(pList,1,(LinkListNode*)&t2); nRect=linkList_Insert(pList,2,(LinkListNode*)&t3); int nLen=linkList_Length(pList); cout<<"共"<<nLen<<"节点"<<endl; for (int i=0;i<nLen;i++) { Teacher *pteacher= (Teacher*)linkList_Get(pList,i); if (pteacher==NULL) { return -1; } cout<<pteacher->name<<" "<<pteacher->nAge<<endl; } cout<<"删除节点"<<endl; while(linkList_Length(pList)>0) { linkList_Del(pList,0); } nLen=linkList_Length(pList); cout<<"共"<<nLen<<"节点"<<endl; for (int i=0;i<nLen;i++) { Teacher *pteacher= (Teacher*)linkList_Get(pList,i); if (pteacher==NULL) { return -1; } cout<<pteacher->name<<" "<<pteacher->nAge<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-26 11:55:54