关于对有头双向链表的头插,头删,尾插,尾删,以及插入,查询的方法代码如下:
整个原代码:
List.h
#pragma once
#include<iostream>
using namespace std;
class ListNode
{
public:
ListNode* begin;
int data;
ListNode* end;
};
void ListInit(ListNode*& p);//链表初始化
void ListPushFront(ListNode*& phead, int x);头插
void Listprintf(ListNode*& phead);打印链表
void ListPushBack(ListNode*& phead, int x);尾插
void ListPopBack(ListNode*& phead);尾删
void ListPopFront(ListNode*& phead);头删
ListNode* SListFind(ListNode*& phead, int x);查找
void ListInsert(ListNode*& phead, ListNode* pos, int x);指定节点前插入
List.cpp
#include<iostream>
#include"List.h"
using namespace std;
void ListInit(ListNode*& p)//初始化
{
p = new ListNode();
p->begin = p;
p->end = p;
p->data = 0;
}
void ListPushFront(ListNode*& phead, int x)//头插
{
ListNode* newnode = new ListNode();
newnode->data = x;
if (phead->begin==phead)
{
phead->end = newnode;
phead->begin = newnode;
newnode->end = phead;
newnode->begin = phead;
}
else
{
newnode->end = phead;
newnode->begin = phead->begin;
phead->begin->end = newnode;
phead->begin = newnode;
}
phead = newnode;
}
void Listprintf(ListNode*& phead)//打印
{
ListNode* head=phead->end;
cout << "phead->begin " << phead->begin << " phead->data " << phead->data << " phead->end " << phead->end << endl;
while (phead!=head)
{
cout << "p->begin=" << head->begin << " p->data=" << head->data << " p->end=" << head->end << endl;
head = head->end;
}
}
void ListPushBack(ListNode*& phead, int x)//尾插
{
ListNode* newnode = new ListNode();
newnode->data = x;
if (phead->begin == phead)
{
phead->end = newnode;
phead->begin = newnode;
newnode->end = phead;
newnode->begin = phead;
}
else {
newnode->begin = phead->begin;
newnode->end = phead;
newnode->begin->end = newnode;
phead->begin = newnode;
}
}
void ListPopBack(ListNode*& phead)
{
phead->begin = phead->begin->begin;
phead->begin->end = phead;
}
void ListPopFront(ListNode*& phead)
{
phead->end->begin = phead->begin;
phead->begin->end = phead->end;
phead = phead->end;
}
ListNode* SListFind(ListNode*& phead, int x)
{
ListNode* head = phead->end;
if (phead->data == x)
{
return phead;
}
while (phead != head)
{
if (head->data == x)
{
return head;
}
head = head->end;
}
return NULL;
}
void ListInsert(ListNode*& phead, ListNode* pos, int x)
{
if (phead->data == pos->data)
{
ListPushFront(phead, x);
}
else
{
ListNode* newnode = new ListNode();
newnode->data = x;
ListNode* head = phead->end;
while (phead != head)
{
if (head->data == pos->data)
{
newnode->begin = head->begin;
newnode->end = head;
head->begin->end = newnode;
head->begin = newnode;
}
head = head->end;
}
}
}
int main()
{
ListNode* p;
ListInit(p);
ListPushBack(p, 1);
ListPushBack(p, 2);
ListPushBack(p, 5);
ListPushBack(p, 6);
Listprintf(p);
ListPopFront(p);
ListPopFront(p);
ListPopFront(p);
ListNode*SIX=SListFind(p, 6);
ListInsert(p, SIX, 22);
Listprintf(p);
}
各个方法如下:
void ListInit(ListNode*& p)//初始化
void ListInit(ListNode*& p)//初始化
{
p = new ListNode();
p->begin = p;
p->end = p;
p->data = 0;
}
void ListPushFront(ListNode*& phead, int x)//头插
void ListPushFront(ListNode*& phead, int x)//头插
{
ListNode* newnode = new ListNode();
newnode->data = x;
if (phead->begin==phead)
{
phead->end = newnode;
phead->begin = newnode;
newnode->end = phead;
newnode->begin = phead;
}
else
{
newnode->end = phead;
newnode->begin = phead->begin;
phead->begin->end = newnode;
phead->begin = newnode;
}
phead = newnode;
}
void Listprintf(ListNode*& phead)//打印
void Listprintf(ListNode*& phead)//打印
{
ListNode* head=phead->end;
cout << "phead->begin " << phead->begin << " phead->data " << phead->data << " phead->end " << phead->end << endl;
while (phead!=head)
{
cout << "p->begin=" << head->begin << " p->data=" << head->data << " p->end=" << head->end << endl;
head = head->end;
}
}
void ListPushBack(ListNode*& phead, int x)//尾插
void ListPushBack(ListNode*& phead, int x)//尾插
{
ListNode* newnode = new ListNode();
newnode->data = x;
if (phead->begin == phead)
{
phead->end = newnode;
phead->begin = newnode;
newnode->end = phead;
newnode->begin = phead;
}
else {
newnode->begin = phead->begin;
newnode->end = phead;
newnode->begin->end = newnode;
phead->begin = newnode;
}
}
void ListPopBack(ListNode*& phead)
void ListPopBack(ListNode*& phead)
{
phead->begin = phead->begin->begin;
phead->begin->end = phead;
}
void ListPopFront(ListNode*& phead)
void ListPopFront(ListNode*& phead)
{
phead->end->begin = phead->begin;
phead->begin->end = phead->end;
phead = phead->end;
}
ListNode* SListFind(ListNode*& phead, int x)
ListNode* SListFind(ListNode*& phead, int x)
{
ListNode* head = phead->end;
if (phead->data == x)
{
return phead;
}
while (phead != head)
{
if (head->data == x)
{
return head;
}
head = head->end;
}
return NULL;
}
void ListInsert(ListNode*& phead, ListNode* pos, int x)
void ListInsert(ListNode*& phead, ListNode* pos, int x)
{
if (phead->data == pos->data)
{
ListPushFront(phead, x);
}
else
{
ListNode* newnode = new ListNode();
newnode->data = x;
ListNode* head = phead->end;
while (phead != head)
{
if (head->data == pos->data)
{
newnode->begin = head->begin;
newnode->end = head;
head->begin->end = newnode;
head->begin = newnode;
}
head = head->end;
}
}
}