用c++写一个单链表

2025-02-14 06:01:35
推荐回答(3个)
回答1:

typedef struct tagTList
{
int m_nKey;
struct tagTList *m_pNext;
tagTList(void)
{
m_nKey = 0;
m_pNext = NULL;
}
tagTList(int nValue)
{
m_nKey = nValue;
m_pNext = NULL;
}
}TList;
void CreateList(TList* &h, int n)
{
TList *pTemp = NULL, *pIT:
h = new TList();
pIT = h;
for (int i=0; i {
pTemp = new TList(i+1);
p->m_pNext = pTemp;
p = pTemp;
}
}

//很简单的,你应该要看明白,看不明白再问。

回答2:

struct node
{
int key;
node *next;
} *h=NULL;

void init(int n)
{
int i=1;
node *p;
node *q;
while(i<=n)

p=new node;
p->key=i;
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
i++;

}

回答3:

diu nei