頭文件如下:
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef void Link;
typedef struct _LinkListNode
{
struct _LinkListNode * next;
}LinkListNode;
Link * LinkList_create();
int LinkList_insert(Link*, int, LinkListNode *);
int LinkList_getlength(Link *);
LinkListNode * LinkList_get(Link *, int);
int LinkList_clear(Link*);
LinkListNode *LinkList_gethead(Link *);
LinkListNode *LinkList_gettail(Link*);
#endif
實現如下:
#include"linklist.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct _TlinkList
{
LinkListNode node;
LinkListNode tail;
int length;
}Tlinklist;
Link * LinkList_create()
{
Tlinklist * list = (Tlinklist*)malloc(sizeof(Tlinklist));
list->length = 0;
list->node.next = NULL;
list->tail.next = NULL;
return list;
}
int LinkList_insert(Link* list, int pos, LinkListNode *node)
{
Tlinklist* tlist = (Tlinklist*)list;
if (list == NULL || node == NULL || pos < 0)
{
return -1;
}
int length = tlist->length;
LinkListNode* p = &tlist->node;
///if(pos >= length)
// {
// pos = length;
// }
int i = 0;
for (i = 0; (i < pos) && p->next != NULL; i++)
{
p = p->next;
}
node->next = p->next;
p->next = node;
//printf("%dNEXT\n",(int)node->next);
// printf("%dNODE\n",(int)node);
tlist->length++;
if (tlist->length == 1 || pos == tlist->length)
{
tlist->tail.next = node;
}
return 5;
}
int LinkList_getlength(Link *list)
{
Tlinklist* tlist = (Tlinklist*)list;
if (list == NULL)
{
return -1;
}
return tlist->length;
}
LinkListNode * LinkList_get(Link *list, int pos)
{
Tlinklist* tlist = (Tlinklist*)list;
LinkListNode * ret = NULL;
LinkListNode * current = &tlist->node;
if (list == NULL)
{
return NULL;
}
int i = 0;
for (i; i< pos; i++)
{
current = current->next;
}
ret = current->next;
return ret;
}
int LinkList_clear(Link* list)
{
return 0;
}
LinkListNode *LinkList_gethead(Link * list)
{
return;
}
LinkListNode * LinkList_gettail(Link* list)
{
return;
}
測試如下:
#include"linklist.h"
#include<stdio.h>
typedef struct _teacher
{
int age;
}Teacher;
int main()
{
Teacher t1;
t1.age = 100;
Teacher t2;
t2.age = 2;
Teacher t3;
t3.age = 3;
Link * list = LinkList_create();
LinkList_insert(list, 0, (LinkListNode*)(&t1));
Teacher * tp = (Teacher*)LinkList_get(list, 0);
printf("%d\n", tp->age);
LinkList_insert(list, 1, (LinkListNode*)(&t2));
LinkList_insert(list, 2, (LinkListNode*)(&t3));
// printf("%d\n",LinkList_getlength(list));
//Teacher * tp =(Teacher*)LinkList_get(list, 0);
printf("%d\n", tp->age);
system("pause");
}
問題是為什麼運行LinkList_insert後tp->age變為0;
你不能夠把Teacher類型來當作LinkListNode類型來操作
按你的意思,應作如下修改:
1. 在LinkListNode結構體裡添加一個成員變量data,以存放用戶數據:
typedef struct _LinkListNode
{
struct _LinkListNode * next;
void * data;
}LinkListNode;
把main函數修改如下:
int main()
{
Teacher t1;
t1.age = 100;
Teacher t2;
t2.age = 2;
Teacher t3;
t3.age = 3;
LinkListNode *pNode1 = new ListListNode();
pNode1->next = null;
pNode1->data = &t1;
LinkListNode *pNode2 = new ListListNode();
pNode2->next = null;
pNode2->data = &t2;
LinkListNode *pNode3 = new ListListNode();
pNode3->next = null;
pNode3->data = &t3;
Link * list = LinkList_create();
LinkList_insert(list, 0, pNode1);
LinkListNode * pCurNode = LinkList_get(list, 0);
Teacher *tp = (Teacher *)pCurNode->data;
printf("%d\n", tp->age);
LinkList_insert(list, 1, pNode2);
LinkList_insert(list, 2, pNode3);
//釋放資源
LinkList_clear();
system("pause");
}
LinkList-clear()也要完善以釋放鏈表裡的資源。
希望能幫到你。