#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
/* 线性表的单链表存储结构 */
typedef struct LNode {
ElemType data;
LNode *next;
} *LinkList;
void InitList(LinkList &L, ElemType a[], int n);
void CreateList(LinkList& L);
void CreateList2(LinkList &L, int n);
void PrintList(LinkList L);
Status GetElem(LinkList L, int i, ElemType &e);
Status InsertElem(LinkList &L, int i, ElemType e);
Status DeleteElem(LinkList &L, int i, ElemType &e);
void MergeList(LinkList &La, LinkList &Lb, LinkList &Lc);
bool IsEmpty(LinkList L);
int ListLength(LinkList L);
LNode *ListTail(LinkList L);
LNode *ListMaxElem(LinkList L);
LNode *Prior(LinkList L, LNode *cur);
void SortList(LinkList &L);
int main(void)
{
ElemType a[] = { 1, 2, 3, 4, 5, 6 };
LinkList LList;
InitList(LList, a, 6);
cout << "ListLength(LList)=" << ListLength(LList) << endl;
cout << "ListTail(LList)=" << ListTail(LList) << endl;
cout << "ListTail(LList)->data=" << ListTail(LList)->data << endl;
cout << "ListMaxElem(LList)=" << ListMaxElem(LList) << endl;
cout << "ListMaxElem(LList)->data=" << ListMaxElem(LList)->data << endl;
LNode *max_ptr = ListMaxElem(LList);
LNode *max_prior = Prior(LList, max_ptr);
cout << "max_prior=" << max_prior << endl;
cout << "max_prior->data=" << max_prior->data << endl;
bool IsEmpty_LList = IsEmpty(LList);
cout << boolalpha << IsEmpty_LList << endl;
PrintList(LList);
int e = 0;
GetElem(LList, 4, e);
cout << "e=" << e << endl;
InsertElem(LList, 3, 33);
PrintList(LList);
int f;
DeleteElem(LList, 5, f);
cout << "f=" << f << endl;
PrintList(LList);
LinkList La, Lb, Lc;
CreateList(La);
CreateList(Lb);
cout << "排序之前 La: ";
PrintList(La);
cout << "排序之前 Lb: ";
PrintList(Lb);
SortList(La);
cout << "排序之后 La: ";
PrintList(La);
SortList(Lb);
cout << "排序之后 Lb: ";
PrintList(Lb);
MergeList(La, Lb, Lc);
cout << "La 和 Lb 归并所得链表 Lc:/n";
PrintList(Lc);
return 0;
}
void InitList(LinkList &L, ElemType a[], int n)
{
LNode *head = new LNode;
LNode *p = L = head;
for (int i = 0; i < n; i++) {
LNode *node = new LNode;
node->data = a[i];
p->next = node;
p = p->next;
p->next = NULL;
}
}
void CreateList(LinkList& L)
{
cout << "请输入一些整数(以'#'结束):/n";
L = new LNode;
LNode *p = L;
int i;
while (cin >> i) {
LNode *node = new LNode;
node->data = i;
p->next = node;
p = p->next;
p->next = NULL;
}
cin.clear();
cin.ignore();
}
void PrintList(LinkList L)
{
LNode *p = L->next;
while (p) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
/* P29 L为带头节点的单链表的头指针,当第i个元素存在时,其值赋给e并返回OK,
* 否则返回ERROR
*/
Status GetElem(LinkList L, int i, ElemType &e)
{
LNode *p = L->next;
int j = 1;
while (p && j < i) {
p = p->next;
j++;
}
if (!p || j > i)
return ERROR;
e = p->data;
return OK;
}
/* 在带头节点的单链线性表L中第i个位置之前插入元素e
*/
Status InsertElem(LinkList &L, int i, ElemType e)
{
LNode *p = L;
int j = 0;
while (p && j < i - 1) {
p = p->next;
++j;
}
if (!p || j > i - 1)
return ERROR;
// LNode *s = (LNode *)malloc(sizeof(LNode));
LNode *s = new LNode;
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}
/* 在带头节点的单链线性表L中,删除第i个元素,并由e返回其值
*/
Status DeleteElem(LinkList &L, int i, ElemType &e)
{
LNode *p = L;
int j = 0;
while (p->next && j < i - 1) {
p = p->next;
++j;
}
if (!p->next || j > i - 1)
return ERROR;
LNode *q = p->next;
p->next = q->next;
e = q->data;
// free(q);
delete q;
return OK;
}
/* P30 逆位序输入n个元素的值,建立带表头节点的单链线性表L
*/
void CreateList2(LinkList &L, int n)
{
// L = (LinkList)malloc(sizeof(LNode));
L = new LNode;
L->next = NULL;
for (int i = n; i > 0; --i) {
// LNode *p = (LNode *)malloc(sizeof(LNode));
LNode *p = new LNode;
// scanf("%d", &p->data);
cout << "Please input an integer:/n";
cin >> p->data;
p->next = L->next;
L->next = p;
}
}
/* P31 已知单链线性表La和Lb的元素按值非递减排列,归并La和Lb得到新的单链线性表Lc,
* Lc的元素也按值非递减排列
*/
void MergeList(LinkList &La, LinkList &Lb, LinkList &Lc)
{
LNode *pa = La->next;
LNode *pb = Lb->next;
LNode *pc = Lc = La;
while (pa && pb) {
if (pa->data <= pb->data) {
pc->next = pa;
pc = pa;
pa = pa->next;
} else {
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa ? pa : pb;
// free(Lb);
delete Lb;
}
bool IsEmpty(LinkList L)
{
if (L->next == NULL)
return true;
return false;
}
int ListLength(LinkList L)
{
LNode *p = L->next;
int n = 0;
while (p) {
++n;
p = p->next;
}
return n;
}
LNode *ListTail(LinkList L)
{
if (L->next == NULL)
return NULL;
LNode *p = L;
while (p->next)
p = p->next;
return p;
}
LNode *ListMaxElem(LinkList L)
{
if (!L->next)
return NULL;
LNode *p = L->next;
LNode *pmax = L->next;
ElemType max = p->data;
while (p) {
if (p->data > max) {
max = p->data;
pmax = p;
}
p = p->next;
}
return pmax;
}
LNode *Prior(LinkList L, LNode *cur)
{
LNode *p = L;
while (p) {
if (p->next == cur)
return p;
p = p->next;
}
return NULL;
}
void SortList(LinkList& L)
{
LNode *p, *pre;
LNode *head = new LNode;
head->next = NULL;
while (L->next) {
p = ListMaxElem(L);
pre = Prior(L, p);
pre->next = p->next;
p->next = head->next;
head->next = p;
}
delete L;
L = head;
}
单链表
最新推荐文章于 2023-05-06 22:43:40 发布