单链表实现(C++)

C++实现单链表数据结构

myList.h

#ifndef MYLIST_H
#define MYLIST_H

typedef struct node
{
	int data;
	struct node *next;
}Node;
class myList
{
public:
	myList();
	~myList();
	void initList();				// 初始化单链表
	int getData(int pos);			//得到pos位置的元素
	void insert(int val, int pos);	//将值为val的元素插入到单链表的pos位置
	int remove(int pos);			//删除第pos个结点,并返回该结点的值
	void reverse();					//对单链表进行转置
	int searchValue(int val);		//寻找值为val的结点在单链表中的位置
	int getLength();				//返回单链表的长度
	void print();				//遍历打印单链表
private:
	Node *head;					//头结点指针	
	Node *cur;					//当前结点指针
	int length;					//单链表长度
};
#endif

myList.cpp

#include <iostream>
#include "myList.h"
using namespace std;

myList::myList()
{
	head = NULL;
	cur = NULL;
	length = 0;
}
myList::~myList()
{
	if (NULL != head)
	{
		cur = head; //从头开始删除结点
		while (0 != length)
		{
			head = cur->next;
			delete cur;
			cur = head;
			length--;
		}

	}
}
void myList::initList()
{
	int val;
	while (cin >> val )
	{
		Node *p = new Node;
		p->data = val;
		p->next = NULL;
		if (NULL == head)	//单链表为空
		{
			head = p;
			cur = p;
		}
		//单链表不为空
		else    
		{
			cur->next = p;
			cur = p;
		}
		length++;
	}
}
int myList::getData(int pos)
{
	if (pos >= length) 
	{
		cerr << "overflow" << endl;
		return (-1);
	}
	else
	{
		cur = head;
		int count = 0;
		while(count != pos)
		{
			cur = cur->next;
			count++;
		}
		cout << cur->data << endl;
		return cur->data;
	}
}
void myList::insert(int val, int pos)
{
	if (pos > length)
	{
		cerr << "pos error" << endl;
	}
	else
	{
		Node *p = new Node;
		p->data = val;
		p->next = NULL;
		if (0 == pos)		//插入到最前面
		{
			cur = head;
			head = p;
			head->next = cur;
		}
		//插入到其他位置
		else
		{
			cur = head;
			int count = 0;
			while (count != pos-1)
			{
				cur = cur->next;
				count++;
			}
			p->next = cur->next;
			cur->next = p;
		}
		length++;
	}
}
int myList::remove(int pos)
{
	if (pos > length && pos < 0)
	{
		cerr << "pos error" << endl;
		return (-1);
	}
	else
	{
		cur = head;
		int count = 0, val;
		if (0 == pos)			//删除首结点
		{
			cur = head->next;
			val = head->data;
			delete head;
			head = cur;
		}
		//删除其他结点
		else
		{
			while (count != pos-1)
			{
				cur = cur->next;
				count++;
			}
			Node *p = cur->next;
			cur->next = cur->next->next;
			val = p->data;
			delete p;
		}
		length--;
		return val;
	}
}
void myList::reverse()
{
	Node *p1, *p2, *p3;
	if (NULL == head || NULL == head->next)
	{
		return;
	}
	else
	{
		p1 = head, p2 = p1->next;
		while(p2)
		{
			p3 = p2->next;
			p2->next = p1;
			p1 = p2;
			p2 = p3;
		}
		head->next = NULL;
		head = p1;
	}
}
int myList::searchValue(int val)
{
	if (length <= 0)
	{
		cerr << val << " is not in list" <<endl;
		return (-1);
	}
	else
	{
		cur = head;
		int count = 0;
		while (NULL != cur)
		{
			if (val == cur->data)
			{
				cout << val << " is in the list , the pos is " << count << endl;
				return count;
			}
			cur = cur->next;
			count++;
		}
		cerr << val << " is not in list" <<endl;
		return (-1);
	}
}
inline int myList::getLength()
{
	cout << "the length of the list is " << length << endl;
	return length;
}
void myList::print()
{
	if (length > 0)
	{
		cur = head;
		cout << "The list is:" << endl;
		while (NULL != cur)
		{
			cout << cur->data << " ";
			cur = cur->next;
		}
		cout << endl;
	}
	else
		cout << "The list is empty!" << endl;
}
int main()
{
	myList ilist;
	ilist.initList();
	ilist.reverse();
	ilist.print();
	ilist.getLength();
	ilist.searchValue(5);
	ilist.getData(5);
	ilist.getData(2);
	ilist.insert(31,3);
	//cout << ilist.remove(1) <<endl;
	ilist.reverse();
	ilist.print();
	ilist.getLength();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值