CPP一个链表,数据的增删改查(不包括翻转、排序,合并等)等,Node* &的使用

来源:互联网 发布:sql select 两个字段 编辑:程序博客网 时间:2024/06/05 18:57
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <Windows.h>
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;
// if (p == head)break;
// }
// cout << endl;
//}
//
//void main1()
//{
// Node a(1), b(2), c(3), d(4), e(5);
// a.next = &b;
// b.next = &c;
// c.next = &d;
// d.next = &e;
//
// showlist(&a);
// b.next = d.next;
// showlist(&a);
// system("pause");
//
//}
typedef int T;
class List
{
//定义节点
struct Node
{
T data;
Node *next;
//const T&d = T() 零初始化,基本类型就是数值0,
//对自定义类型而言就是建个对象不穿参数,适应所有类型
Node(const T&d = T()) :data(d),next(NULL){}
};
Node *head;//头指针
int len ;
public:
List() :head(NULL),len(0)//链表构造函数
{
Node n;
}
/**
前面插入数据
*/
void push_front(const T&d)
{
//使用new 在push_front函数退出后,节点不会消失,调用delete才失去
/* Node *p = new Node(d);
//头部插入
p->next = head;
head = p;*/
//可以改变成:
insert(d, 0);
}


/**
尾部插入数据
*/
void push_back(const T&d)
{
insert(d, len);
}
/**
连续.push 尾部插入数据
*/
List& continue_push_back(const T&d)
{
insert(d, len);
return *this;
}
/*
Node*&  &与Node* 没有关系,&引用,代表直接使用链表中的值而不是副本
链表中指定pos位置的指针
*/
Node *&getptr(int pos)
{
//当插入的位置小于0时,默认是头插入,插入位置大于末尾位置,就默认插入到尾位置!
if (pos <= 0)return head;
Node *p = head;
for (int i = 1; i < pos&&p->next!=NULL; i++)
{
p = p->next; 
}
return p->next;
}
/*
任意位置插入
*/
void insert(const T&d, int pos)
{
//没插入一个节点,len加1;
len++;
Node *p = new Node(d);
Node *&pn = getptr(pos);//pn是原始数据的别名
p->next = pn;
pn = p;
}
/*
删除节点(按照位置删除)
*/
void erase(int pos)
{
if (pos <0 || pos >len-1) return;
Node *&pn = getptr(pos);
Node *p = pn;//另存一份pn  则在定义Node *p 的时候不要定义为:Node* &p;这样的定义意思就是pn的别名,二者是一样的地址。
pn = pn->next;
delete p;
p = NULL;
len--;//节点的数量减少1个;
}
/*
根据数据查找位置
*/
int find(const T&d)const
{
int pos = 0;
Node *p = head;
while (p)
{
if (p->data == d)
return pos;
pos++;
p = p->next;
}
return -1;
}
/*
删除节点(按照数值)
*/
void remove(const T&d)
{
//先找到这个数据的位置
int pos_find = find(d);
if (pos_find != -1)
{
erase(pos_find);
}
}
/*
设置节点数据
*/
void set(int pos, const T&d)
{
if (pos <0 || pos >=len)return;
getptr(pos)->data = d;
}
/*
遍历数据
*/


void showlist()
{
Node *p = head;
while (p != NULL)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
/*
判断是否为空
*/
bool isempty()const
{
return head == NULL;
}
/*
清空整个链表
*/
void clear()
{
while (!isempty())
{
Node *p = head->next;
delete head;
head = p;
}
len = 0;//清除节点后,len设置为0
}
/*
front();找到头节点
*/
T front()const
{
if (isempty())throw "空";
return head->data;
}


/*
back;找到尾部节点
*/
T back()const
{
Node *p = head;
if (isempty())throw "空";
while (p->next!=NULL)
{
p = p->next;
}
return p->data ;
}


/*
计算链表中节点个数
*/


int size()const
{
return len;
}
/*
析构
*/
~List()
{
clear();
}
};

int main()
{
List l;
l.push_front(1);
l.push_front(4);
l.push_front(16);
l.insert(12, 6);
l.insert(13, -1);
l.push_back(22);
l.push_back(23);
l.push_back(24);
l.push_back(25);
//连续多次插入
l.continue_push_back(31).continue_push_back(32).continue_push_back(33);
l.set(l.size()-1, 888);
l.showlist();
l.remove(16);
//l.remove(22);
l.showlist();
printf("%d\n", l.size());
printf("%d\n", l.isempty());
cout << "头部: " << l.front() << "  ,尾部: " << l.back() << endl;
return 0;
}



0 0
原创粉丝点击