数据结构和算法——链表

1.数组不适合在中间或前面增加或删除数据,只适合在末尾增加或删除数据  优点是挨着放,访问起来很快  链表:数据+指针

2.节点

node.cpp

<span style="font-size:18px;">#include <iostream>
using namespace std;
typedef int T;
struct Node{
	T data;
	Node* next;
	Node(const T& d):data(d),next(NULL){}
	operator T(){return data;}
};
void showlist(Node* head)
{
	Node* p = head;
	while(p!=NULL){
		cout << *p << ' ';
		p = p->next;//(*p).next;
//		if(p==head) break;
	}
	cout << endl;
}
int main()
{
	Node a(10), b(20), c(30), d(40), e(50), f(60);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	showlist(&a);
	e.next = b.next;//&c;
	b.next = &e;
	Node*& q = a.next;
	f.next = q;//q==a.next==&b
	q = &f; //a.next = &f
	showlist(&a);
	Node* k = new Node(70);
	Node*& r = c.next;
	k->next = r;
	r = k;
	showlist(&a);
	delete k;
}</span>



3.链表的增删查改

list.cpp

<span style="font-size:18px;">#include <iostream>
  using namespace std;

  typedef int T;

  class List
  {
     struct Node
     {
       T data;
       Node* next;
       Node(const T& d=T()):data(d),next(0){}//零初始化,适用所有类型
     };
     Node* head;//头指针,用来保存头节点的地址
     int len;

   public:
	List():head(NULL),len(0){ }
	void push_front(const T& d)   //前插
	{   
		insert(d,0);
	}
	List& push_back(const T& d)   //尾插
	{   
		insert(d,size());
		return *this;
	}
	int size()const
	{
		return len;
	}
	Node*& getptr(int pos)   //找链表中指向指定位置的指针
	{   
		if(pos<0||pos>size()) pos=0;
		if(pos==0) return head;
		Node* p = head;
		for(int i=1; i<pos; i++)
			p = p->next;
		return (*p).next;
	}
	//1.在链表里找到指向那个位置的指针
	//2.让新节点的next跟找到的那个位置的指针指向同一个地方
	//3.让找到的指针指向新节点
	void insert(const T& d, int pos)    //在任意位置插入
	{  
		Node*& pn = getptr(pos);
		Node* p = new Node(d);
		p->next = pn;
		pn = p;
		++len;
	}
	void travel()const   //遍历
	{    
		Node* p = head;
		while(p!=NULL){
			cout << p->data << ' ';
			p = p->next;
		}
		cout << endl;
	}
	void clear()   //清空这个链表
	{
		while(head!=NULL){
			Node* p = head->next;
//			cout << "delete " << head << endl;
			delete head;
			head = p;
		}
		len = 0;
	}
	~List()
	{
//		cout << this << "*******" << head << endl;
		clear();
	}
	void erase(int pos)   //按位置来删除
	{   //有效位置为0~size()-1  
		if(pos<0||pos>=size()) return;
		Node*& pn = getptr(pos);
		Node* p = pn;
		pn = pn->next;
		delete p;
		--len;
	}
	void remove(const T& d)  //按数值来删除
	{
		int pos;
		while((pos=find(d))!=-1)
			erase(pos);
	}
	int find(const T& d)const   //查找
	{
		int pos = 0;
		Node* p = head;
		while(p){
			if(p->data==d) return pos;
			p = p->next; ++pos;
		}
		return -1;
	}
	void set(int pos, const T& d)  //修改
	{
		if(pos<0||pos>=size()) return;
		getptr(pos)->data = d;
	}

	bool empty()const   //是否为空
	{  return head==NULL;  }

	T front()const
        {
	   if(empty())throw "空";
	   return head->data;
        }

	T back()const
	{
		if(empty())throw "空";
		Node* p=head;
		while(p->next!=NULL)
			p = p->next;
		return p->data;
	}
   };

  int main()
  {
	List l;
	l.push_front(5);//5
	l.push_front(8);//8 5
	l.push_front(20);//20 8 5
	l.insert(9,2);//20 8 9 5
	l.insert(6,100);//6 20 8 9 5
	l.insert(7,-10);//7 6 20 8 9 5
	l.insert(1,2);//7 6 1 20 8 9 5
	l.push_back(10).push_back(15).travel();
	l.erase(0);//x7:6 1 20 8 9 5 10 15
	l.erase(l.size()-1);//x15:6 1 20 8 9 5 10
	l.erase(2);//x20:6 1 8 9 5 10
	l.travel();
	l.push_back(6);//6 1 8 9 5 10 6
	l.insert(6, 3);//6 1 8 6 9 5 10 6
	l.travel();
	l.remove(6);//1 8 9 5 10
	l.travel();
	l.set(0, 666);
	l.set(4, 789);
	l.set(l.find(9),123);
	l.set(1, 777);
	l.travel();
	cout << l.front() << "..." << l.back() << ',' << l.size() << "个" << endl;
	while(!l.empty()) l.erase(0);
	cout << "size:" << l.size() << endl;
	return 0;
  }</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值