带头结点的链表尾插法C++实现

#include 

using namespace std;

typedef int DATA;

//带头结点的链表的尾插法 
struct SNode 
{
	DATA data;
	SNode* pNext;
};

struct SList
{
	SNode* m_pHead;
	SNode* end_pNode;

	SList()
	{
		m_pHead   = new SNode;
		end_pNode = m_pHead;
	}
	~SList()
	{
		SNode* p = m_pHead;
		SNode* p1;
		while (p)
		{
			p1 = p;
			p = p->pNext;
			delete p1;
		}

	}
	void AddHead(DATA d)
	{
		SNode* p = new SNode;
		p->data  = d;
		end_pNode->pNext = p;
		end_pNode  = p;
		end_pNode->pNext  = NULL;
	}

	void Delete_Data()//删除数据
	{
		SNode* p = m_pHead;
		if(p != NULL)
		{
			m_pHead = m_pHead->pNext;
			delete p;
		}

	}

	int GetCount()
	{
		SNode* p = m_pHead->pNext;
		int i = 0;
		while(p)
		{
			i++;
			p=p->pNext;
		}

		return i;
	}

	void prints()
	{
		SNode* p = m_pHead->pNext;

		while (p )
		{
			cout<data<pNext;
		}
	}




};

int main()
{
	SList list;
	while(true)
	{
		cout<<"请输入一个数据:"<>i;
		if (i == 0)
		{
			break;
		}
		list.AddHead(i);
	}

	list.prints();
	cout<<"链表的长度为:"<

你可能感兴趣的:(c/c++)