链表

class TestLink {
class Entry {
public:
Entry() { next = NULL; }
~Entry() {}
Entry(int data) { this->data = data; next = NULL; }
public:
int data;
TestLink::Entry *next;
};
public:
TestLink() { head = new Entry();}
~TestLink() {}
void insertHead(int val) {//1.头插
Entry *cur = new Entry(val);//插入的点
if (head->next == NULL)
{
head->next=cur ;
cur->next = NULL;
}
else
{
cur->next = head->next;//把插入的点next指向head的下一个节点
head->next = cur;//head的next指向插入的点
}
}
void insertTail(int val) {//2.尾插
Entry *goal = new Entry(val);//要插入的点
Entry *cur = head;//用来遍历链表
while (cur->next != NULL) {//当cur.next为null即cur已经在链表尾部,退出循环
cur = cur->next;
}
cur->next = goal;//尾插
goal->next = NULL;
}
int getLength() {//3.获得链表长度
Entry *cur = head;//用来遍历链表
int count = 0;//计数器
while (cur->next != NULL) {
cur = cur->next;
count++;
}
return count;
}
void insert(int val, int post) {//4.任意位置插入
Entry *cur = head;//用来遍历链表
if (post >= 1 && post <= this->getLength()) {//找到插入的位置前的的那一个节点
for (int i = 1; i < post; i++) {
cur = cur->next;
}
}
Entry *entry = new Entry(val);
entry->next = cur->next;//插入
cur->next = entry;
}


void  reverse() {//5.链表的逆置
Entry *p1, *p2 ,*pp;
p1 = head->next;//断开链前保存节点
head->next = NULL;
pp = NULL;
while (p1) {
p2 = p1->next;
p1->next = pp;
pp = p1;
p1 = p2;
}
head->next = pp;
}


Entry* getPost(int k) {//6.求倒数第k个节点
Entry* cur = head;//用来遍历链表
if (k < 1 || k > this->getLength()) {//如果插入的位置小于1或大于链表长度返回NULL
return NULL;
}
for (int i = 0; i < this->getLength() - k + 1 ; i++) {//找到倒数第k个位置
cur = cur->next;
}
return cur;
}
bool isCut(TestLink* t1, TestLink* t2) {//7.求两个链表是否相交
Entry* head1 = t1->head;
Entry* head2 = t2->head;
int len1 = t1->getLength();
int len2 = t2->getLength();
int myLen = len1 - len2;//两条链表的长度差
if (myLen < 0) {//head1指向长链表表头
head1 = t2->head;
head2 = t1->head;
}
for (int i = 0; i < myLen; i++) {//head1到在长链表上移动至和端链表对齐
head1 = head1->next;
}
while (head1 != NULL && head2 != NULL && head1 != head2) {//两个遍历节点同时向后移,如果到尾部结束循环或两个节点相交结束循环
head1 = head1->next;
head2 = head2->next;
}
if (head1 == head2) {//相交返回true
return true;
}
return false;
}
bool judgeLoop() {//8.判断链表是否有环,并且求入口点和环长
Entry *fast = head;//快节点
Entry *slow = head;//慢节点
while (fast->next != NULL && fast->next->next != NULL) {//当快节点到尾部的时候循环结束,因为快节点每次要后移两格所以要加上fast.next.next不等于空的条件,否则会产生空指针异常
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {//相交返回true
return true;
}
}
return false;
}
int intoLoop() {//入口点
Entry *fast = head;
Entry *slow = head;
while (fast->next != NULL && fast->next->next != NULL) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
break;
}
}
slow = head;
while (fast != slow) {
fast = fast->next;
slow = slow->next;
}
return slow->data;
}
int getLoopLength() {//环长 当快慢节点第二次相遇时,慢节点从第一次到第二次相遇走的距离即环长。
Entry* fast = head;
Entry* slow = head;
bool  tag = false;
int length = 0;
while (fast->next != NULL && fast->next->next != NULL) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow && tag == true) {
break;
}
if (fast == slow && tag == false) {
tag = true;
}
if (tag == true) {
length++;
}
}
return length;
}
void printLInk() {
Entry *p = head;
if (p->next == NULL)
{
cout << "空链表!" << endl;
}
while (p->next!=NULL)
{
p = p->next;
cout << p->data << "\t" << endl;
}
}
private:
Entry *head;
};
//insertHead insertTail getLength insert reverse getPost isCut judgeLoop
int main()
{
//node *pHead = listUser(3);
//findMax(pHead);
TestLink link;
link.insertHead(2);
link.insertTail(1);
link.insert(3, 1);
link.printLInk();
link.reverse();
link.printLInk();
auto lin= link.getPost(1);
cout << lin->data << endl;


link.reverse();


system("pause");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值