#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include
#include
using namespace std;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
/**
测试驱动
int array[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int n = 10;
//LinkList head = createSequenceList(array, n);
LinkList head = createReverseList(array, n);
include 写法
#include "D:\DataStructure\PublicLibrary\LinkList\LinkList.h"
*/
/**
顺序建立带有表头结点的单链表-尾插法
*/
LinkList createSequenceList(ElemType array[], int n);
/**
逆序建立带有表头结点的单链表-头插法
*/
LinkList createReverseList(ElemType array[], int n);
/**
遍历单链表
*/
void displayLinkList(LinkList head);
/**
单链表排序
flag 为 1 代表升序 为 0 代表降序
*/
bool sortLinkList(LinkList head, int flag);
/**
在单链表中删除指定第i个的节点 ( 1 <= i <= n), 并且使用引用返回被删除的元素
*/
bool deleteAElement(LinkList head, ElemType &deleteElem, int location);
/**
在单链表中在指定第i个的位置 ( 1 <= i <= n+1)
*/
bool insertAElement(LinkList head, ElemType insertElem, int location);
/**
判断带有头结点的单链表是否为空
*/
bool isEmpty(LinkList head);
bool deleteAElement(LinkList head, ElemType &deleteElem, int location)
{
if(isEmpty(head))
{
cout << "单链表为空,删除失败" << endl;
return false;
}
if(location > head->data || location <= 0)
{
cout << "删除位置非法" << endl;
return false;
}
LinkList p = head->next;
LinkList q = head;
// 循环结束后p执行被删除节点
// 循环结束后q指向p的前一个节点
while(location != 1)
{
q = p;
p = p->next;
location--;
}
deleteElem = p->data;
q->next = p->next;
free(p);
p = NULL;
head->data--;
return true;
}
bool insertAElement(LinkList head, ElemType insertElem, int location)
{
if(location <= 0 || location > head->data+1)
{
cout << "插入位置非法" << endl;
return false;
}
LinkList p = head;
// 循环结束后p指向需要插入位置的前一个节点(即为指向底location-1节点)
while(location != 1)
{
p = p->next;
location--;
}
LinkList insertNode = (LinkList)malloc(sizeof(LNode));
if(insertNode == NULL)
{
cout << "创建节点失败" << endl;
return false;
}
insertNode->data = insertElem;
insertNode->next = p->next;
p->next = insertNode;
head->data++;
return true;
}
LinkList createSequenceList(ElemType array[], int n)
{
LinkList head = (LinkList)malloc(sizeof(LNode));
head->data = 0;
head->next = NULL;
LinkList temp = head;
if(head == NULL)
{
cout << "创建结点失败" << endl;
return NULL;
}
for(int i = 0; i < n; i++)
{
LinkList p = (LinkList)malloc(sizeof(LNode));
p->data = array[i];
temp->next = p;
temp = p;
}
temp->next = NULL;
head->data = n;
return head;
}
void displayLinkList(LinkList head)
{
LinkList p = head->next;
while(p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
return;
}
LinkList createReverseList(ElemType array[], int n)
{
LinkList head = (LinkList)malloc(sizeof(LNode));
head->data = 0;
head->next = NULL;
if(head == NULL)
{
cout << "创建结点失败" << endl;
return NULL;
}
for(int i = 0; i < n; i++)
{
LinkList p = (LinkList)malloc(sizeof(LNode));
p->data = array[i];
p->next = head->next;
head->next = p;
}
head->data = n;
return head;
}
bool sortLinkList(LinkList head, int flag)
{
if(flag == 1)
{
int nodeCount = head->data;
LinkList p = head->next;
for(int i = 0; i < nodeCount-1; i++)
{
LinkList q = head->next;
bool isSort = true;
for(int j = 0; j < nodeCount-1-i; j++)
{
if(q->data > q->next->data)
{
isSort = false;
ElemType temp = q->data;
q->data = q->next->data;
q->next->data = temp;
}
q = q->next;
}
if(isSort == true)
{
break;
}
p = p->next;
}
}
else if(flag == 0)
{
int nodeCount = head->data;
LinkList p = head->next;
for(int i = 0; i < nodeCount-1; i++)
{
LinkList q = head->next;
bool isSort = true;
for(int j = 0; j < nodeCount-1-i; j++)
{
if(q->data < q->next->data)
{
isSort = false;
ElemType temp = q->data;
q->data = q->next->data;
q->next->data = temp;
}
q = q->next;
}
if(isSort == true)
{
break;
}
p = p->next;
}
}
else
{
cout << "传参非法, 排序失败" << endl;
return false;
}
return true;
}
bool isEmpty(LinkList head)
{
if(head->next == NULL)
{
return true;
}
return false;
}
#endif // LINKLIST_H_INCLUDED