一、单链表增删改查代码实现
#include<iostream>
using namespace std;
typedef struct Node {
struct Node* pNext;
int data;
}NODE, * pNODE;
/*创建单链表,头结点不存放任何数据*/
pNODE CreateSingleList(void) {
int length, element;
pNODE pHead = (pNODE)malloc(sizeof(NODE));
if (NULL == pHead) {
cout << "内存分配失败!" << endl;
exit(EXIT_FAILURE);
}
pHead->data = 0;
pHead->pNext = NULL;
pNODE pCurrent = pHead;
cin >> length;
cout << "所创建的单链表的长度:" << length << endl;
for (int i = 1; i <= length; i++) {
pNODE pNew = (pNODE)malloc(sizeof(NODE));
cin >> element;
pNew->data = element;
pNew->pNext = NULL;
pCurrent->pNext = pNew;
pCurrent = pCurrent->pNext;
}
return pHead;
}
/*单链表反转*/
pNODE InvertSingleList(pNODE pHead) {
pNODE pInverseHead = (pNODE)malloc(sizeof(NODE));
if (NULL == pInverseHead) {
cout << "内存分配失败!" << endl;
exit(EXIT_FAILURE);
}
pInverseHead->data = 0;
//pInverseHead->pNext = NULL;
pNODE pCurrent = pHead->pNext;
pNODE pPre = NULL;
while (pCurrent) {
pNODE pnode = pCurrent->pNext;
pCurrent->pNext = pPre;
pPre = pCurrent;
pCurrent = pnode;
}
pInverseHead->pNext = pPre;
return pInverseHead;
}
/*单链表打印*/
void PrintSingleList(pNODE phead) {
pNODE pCurrent = phead->pNext;
cout << "链表元素如下:" << endl;
while (pCurrent != NULL) {
cout << pCurrent->data << " ";
pCurrent = pCurrent->pNext;
if (!pCurrent) cout << " NULL " << endl;
}
}
/*单链表长度计算*/
int CalaulateSingleListLength(pNODE phead) {
int length = 0;
pNODE pCurrent = phead->pNext;
while (pCurrent != NULL) {
pCurrent = pCurrent->pNext;
length++;
}
return length;
}
/*插入单链表元素*/
int InsertSingleList(pNODE phead, int pos, int data) {
pNODE pInsertNode = pNODE(malloc(sizeof(NODE)));
if (NULL == pInsertNode) {
cout << "pInsertNode 内存分配失败!" << endl;
exit(EXIT_FAILURE);
}
pNODE pCurrent = phead->pNext;
pInsertNode->data = data;
for (int i = 1; i < pos - 1; i++) {
pCurrent = pCurrent->pNext;
}
pInsertNode->pNext = pCurrent->pNext;
pCurrent->pNext = pInsertNode;
return 0;
}
/*删除单链表元素*/
int DeleteSingleList(pNODE phead, int pos) {
pNODE pCurrent = phead->pNext;
if (1 == pos) {
phead->pNext = phead->pNext->pNext;
}
else if (2 == pos) {
phead->pNext->pNext = phead->pNext->pNext->pNext;
}
else {
for (int i = 1; i < pos - 1; i++) {
pCurrent = pCurrent->pNext;
}
pCurrent->pNext = pCurrent->pNext->pNext;
}
return 0;
}
/*判断是否存在环形链表*/
bool IsLoopExist(pNODE phead) {
pNODE pCurrent = phead->pNext;
pNODE pFastNode = phead->pNext;
pNODE pSlowNode = phead->pNext;
if (pCurrent->pNext == NULL || pCurrent == NULL) {
return false;
}
while (pFastNode != NULL && pFastNode->pNext != NULL) {
pFastNode = pFastNode->pNext->pNext;
pSlowNode = pSlowNode->pNext;
if (pFastNode == pSlowNode)
return true;
}
return false;
}
/*寻找链表中的中间元素*/
pNODE SearchMiddleNode(pNODE phead) {
int length = 0 ;
pNODE pCurrent = phead->pNext;
length = CalaulateSingleListLength(phead);
cout << "链表长度:" << length << endl;
if (length % 2 == 0) {
length = length / 2;
}
else
length = length/2 + 1;
while (--length)
pCurrent = pCurrent->pNext;
return pCurrent;
}
int main(int argc, char* argv[]) {
pNODE head = NULL;
head = CreateSingleList();
PrintSingleList(head);
pNODE InverseHead = InvertSingleList(head);
PrintSingleList(InverseHead);
InsertSingleList(InverseHead, 4, 100);
PrintSingleList(InverseHead);
DeleteSingleList(InverseHead, 4);
PrintSingleList(InverseHead);
}
二、测试验证
9
所创建的单链表的长度:9
1 2 3 4 5 6 7 8 9
链表元素如下:
1 2 3 4 5 6 7 8 9 NULL
链表元素如下:
9 8 7 6 5 4 3 2 1 NULL
链表元素如下:
9 8 7 100 6 5 4 3 2 1 NULL
链表元素如下:
9 8 7 6 5 4 3 2 1 NULL