如何创建链表

#include 
using namespace std;
typedef struct Node
{
	int value ;
	struct Node* pNext;
}*PNode,NODE;

class List
{
public:
	List(){ m_pHead = NULL;}
	void add(int elem);
	void travel();
	//~List();
private:
	PNode m_pHead;
	PNode m_pNew;
	PNode m_pTail;
};
void List::add(int elem)
{
	if (m_pHead == NULL)//空链表
	{
		m_pHead = new NODE;
		m_pHead->value = elem;
		m_pHead->pNext = NULL;
		m_pTail = m_pHead;
	}
	else
	{
		m_pNew = new NODE;
		m_pNew->value = elem;
		m_pNew->pNext = NULL;
		m_pTail->pNext = m_pNew;
		m_pTail = m_pTail->pNext;
	}
	
}

void List::travel()
{
	PNode pNode = m_pHead;
	while (pNode != NULL)
	{
		cout << pNode->value << " ";
		pNode = pNode->pNext;
	}
}

int main()
{
	List list;//空表 
	for (int i = 0; i < 10; ++i)
		list.add(i);
	list.travel();
	system("pause") ;
	return 0 ;
}
后续会加上删除,插入等函数

你可能感兴趣的:(数据结构)