C语言如何建立一个列表,编程怎么编,举例说一下,谢谢啦

如题所述

首先定义一个链表。

struct node
{
int id;
struct node * next;

};

接下来写一些操作的函数(插入,删除等等)。

插入函数:

struct node* insert(struct node* pNode, int iTemp)
{
//判断 pNode 是否为空。
if(pNode==NULL)
{
//这个节点是空,返回错误。
return NULL;

}
// 创建一个新的节点。

struct node* tempNode = (struct node*)malloc(sizeof(struct node));
tempNode->id= iTemp;
if(pNode->next == NULL)
{

pNode->next = tempNode;
tempNode->next = NULL;

}else
{
struct node * pNext = pNode->next;
pNode->next = tempNode;
tempNode->next = pNext;

}

return tempNode;

}

int main()
{
struct node* head = (struct node*)malloc(sizeof(struct node));
head->id = 0;
head->next = NULL;
struct node * ptemp;
ptemp = head;

for( int i=1; i<10; i++)
{
struct node* temp = insert(ptemp,i);
ptemp = temp;

}

return 0;

}追问

那怎么知道一个列表的长度呢,比如说一个长度为10的列表,最好详细一点

追答

从链表的头开始,计算。

int LengthOf(struct node * pHead)
{
int i =0;
struct node* head = pHead;

while(head!=NULL)
{
head = head->next; //

i++;

}

return i;

}

追问

那对于顺序表呢?应该怎么弄啊?
int Length(sequenlist L)
{return L.len;}这个怎么用啊?

追答

兄弟,自己想想吧。。。。。。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-01-13
VC ,cListCtrl