#include <iostream>
#include "SqList.h"
#include "LinkList.h"
using namespace std;
// Forward declaration to avoid compilation errors
class SqList;
// Test sequential list
void testSqList() {
cout << "\n===== Sequential List Test =====" << endl;
SqList sq;
ElemType e, data[10] = {'s','a','d','f','g','h','j','k','l','p'};
Status re;
// Initialize
re = sq.InitList();
cout << "Initialize list " << (re == OK ? "success" : "failed") << endl;
// Insert elements
for (int i = 1; i <= 10; i++) {
sq.ListInsert(i, data[i-1]);
}
cout << "After inserting 10 elements, length: " << sq.ListLength() << endl;
// Find element
int pos = sq.LocateElem('f');
cout << "'f' position: " << (pos ? pos : -1) << endl;
// Insert element
re = sq.ListInsert(3, 'x');
cout << "Insert 'x' at position 3 " << (re == OK ? "success" : "failed") << endl;
cout << "After insertion, length: " << sq.ListLength() << endl;
sq.GetElem(3, e);
cout << "Element at position 3: " << e << endl;
// Delete element
re = sq.ListDelete(5, e);
cout << "Delete element at position 5 " << (re == OK ? "success, deleted value: " : "failed") << e << endl;
cout << "After deletion, length: " << sq.ListLength() << endl;
// Get predecessor and successor
re = sq.PriorElem('d', e);
cout << "'d' predecessor: " << (re == OK ? string(1, e) : "none") << endl;
re = sq.NextElem('d', e);
cout << "'d' successor: " << (re == OK ? string(1, e) : "none") << endl;
// Destroy
sq.DestroyList();
}
// Test linked list
void testLinkList() {
cout << "\n===== Linked List Test =====" << endl;
LinkListClass ll;
ElemType e, data[10] = {'s','a','d','f','g','h','j','k','l','p'};
Status re;
// Initialize
re = ll.InitList();
cout << "Initialize list " << (re == OK ? "success" : "failed") << endl;
// Insert elements
for (int i = 1; i <= 10; i++) {
ll.ListInsert(i, data[i-1]);
}
cout << "After inserting 10 elements, length: " << ll.ListLength() << endl;
// Find element
int pos = ll.LocateElem('f');
cout << "'f' position: " << (pos ? pos : -1) << endl;
// Insert element
re = ll.ListInsert(3, 'x');
cout << "Insert 'x' at position 3 " << (re == OK ? "success" : "failed") << endl;
cout << "After insertion, length: " << ll.ListLength() << endl;
ll.GetElem(3, e);
cout << "Element at position 3: " << e << endl;
// Delete element
re = ll.ListDelete(5, e);
cout << "Delete element at position 5 " << (re == OK ? "success, deleted value: " : "failed") << e << endl;
cout << "After deletion, length: " << ll.ListLength() << endl;
// Get predecessor and successor
re = ll.PriorElem('d', e);
cout << "'d' predecessor: " << (re == OK ? string(1, e) : "none") << endl;
re = ll.NextElem('d', e);
cout << "'d' successor: " << (re == OK ? string(1, e) : "none") << endl;
// Destroy
ll.DestroyList();
}
int main() {
testSqList();
testLinkList();
system("pause");
return 0;
}
#include "LinkList.h"
#include <stdlib.h>
// Initialize linked list (with head node)
Status LinkListClass::InitList() {
head = (LinkList)malloc(sizeof(LNode)); // Allocate head node
if (!head) exit(OVERFLOW);
head->next = NULL; // Head node points to null
return OK;
}
// Destroy linked list
Status LinkListClass::DestroyList() {
LinkList p;
while (head) {
p = head;
head = head->next; // Move head pointer
free(p); // Release current node
}
return OK;
}
// Clear linked list (retain head node)
Status LinkListClass::ClearList() {
LinkList p = head->next, q;
head->next = NULL; // Head node points to null
while (p) {
q = p->next;
free(p); // Release data nodes
p = q;
}
return OK;
}
// Check if list is empty
Status LinkListClass::ListEmpty() {
return (head->next == NULL) ? TRUE : FALSE;
}
// Get list length
int LinkListClass::ListLength() {
int len = 0;
LinkList p = head->next;
while (p) {
len++;
p = p->next;
}
return len;
}
// Get the i-th element's value
Status LinkListClass::GetElem(int i, ElemType& e) {
LinkList p = head->next;
int j = 1;
while (p && j < i) { // Find the i-th node
p = p->next;
j++;
}
if (!p || j > i) return ERROR; // Invalid i
e = p->data;
return OK;
}
// Find position of element e
int LinkListClass::LocateElem(ElemType e) {
LinkList p = head->next;
int i = 1;
while (p) {
if (p->data == e) return i; // Found, return position
p = p->next;
i++;
}
return 0; // Not found
}
// Get predecessor of e
Status LinkListClass::PriorElem(ElemType e, ElemType& pree) {
LinkList p = head->next, pre = head; // pre points to p's predecessor
while (p->next) {
if (p->next->data == e) { // p is predecessor of e
pree = p->data;
return OK;
}
pre = p;
p = p->next;
}
return ERROR; // e is first element or not exists
}
// Get successor of e
Status LinkListClass::NextElem(ElemType e, ElemType& nexe) {
LinkList p = head->next;
while (p && p->next) {
if (p->data == e) { // p's next is successor of e
nexe = p->next->data;
return OK;
}
p = p->next;
}
return ERROR; // e is last element or not exists
}
// Insert element e at position i
Status LinkListClass::ListInsert(int i, ElemType e) {
LinkList p = head;
int j = 0;
while (p && j < i - 1) { // Find (i-1)-th node
p = p->next;
j++;
}
if (!p || j > i - 1) return ERROR; // Invalid i
LinkList s = (LinkList)malloc(sizeof(LNode)); // Allocate new node
if (!s) exit(OVERFLOW);
s->data = e;
s->next = p->next; // New node points to original i-th node
p->next = s; // (i-1)-th node points to new node
return OK;
}
// Delete i-th element, save value to e
Status LinkListClass::ListDelete(int i, ElemType& e) {
LinkList p = head, q;
int j = 0;
while (p->next && j < i - 1) { // Find (i-1)-th node
p = p->next;
j++;
}
if (!p->next || j > i - 1) return ERROR; // Invalid i
q = p->next; // q points to node to be deleted
e = q->data; // Save deleted value
p->next = q->next; // Bypass the deleted node
free(q); // Release memory
return OK;
}#ifndef LINKLIST_H
#define LINKLIST_H
#include "status.h"
typedef struct LNode {
ElemType data;
struct LNode* next;
} LNode, *LinkList;
class LinkListClass {
private:
LinkList head; // 头指针(带头节点)
public:
Status InitList(); // 初始化链表
Status DestroyList(); // 销毁链表
Status ClearList(); // 清空链表
Status ListEmpty(); // 判断是否为空
int ListLength(); // 获取长度
Status GetElem(int i, ElemType& e); // 获取第i个元素
int LocateElem(ElemType e); // 查找元素e的位置
Status PriorElem(ElemType e, ElemType& pree); // 获取前驱
Status NextElem(ElemType e, ElemType& nexe); // 获取后继
Status ListInsert(int i, ElemType e); // 插入元素
Status ListDelete(int i, ElemType& e); // 删除元素
};
#endif#ifndef STATUS_H
#define STATUS_H
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
#ifndef SQLIST_H
#define SQLIST_H
#include "status.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
class SqList {
private:
ElemType* elem;
int length;
int listsize;
public:
Status InitList() {
elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!elem) exit(OVERFLOW);
length = 0;
listsize = LIST_INIT_SIZE;
return OK;
}
Status DestroyList() {
free(elem);
elem = NULL;
length = 0;
listsize = 0;
return OK;
}
Status ClearList() {
length = 0;
return OK;
}
Status ListEmpty() {
return (length == 0) ? TRUE : FALSE;
}
int ListLength() {
return length;
}
Status GetElem(int i, ElemType& e) {
if (i < 1 || i > length) return ERROR;
e = elem[i - 1];
return OK;
}
int LocateElem(ElemType e) {
for (int i = 0; i < length; i++) {
if (elem[i] == e) return i + 1;
}
return 0;
}
Status PriorElem(ElemType e, ElemType& pree) {
int i = LocateElem(e);
if (i <= 1) return ERROR;
pree = elem[i - 2];
return OK;
}
Status NextElem(ElemType e, ElemType& nexe) {
int i = LocateElem(e);
if (i == 0 || i == length) return ERROR;
nexe = elem[i];
return OK;
}
Status ListInsert(int i, ElemType e) {
if (i < 1 || i > length + 1) return ERROR;
if (length >= listsize) {
ElemType* newbase = (ElemType*)realloc(elem, (listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
elem = newbase;
listsize += LISTINCREMENT;
}
for (int j = length; j >= i; j--) {
elem[j] = elem[j - 1];
}
elem[i - 1] = e;
length++;
return OK;
}
Status ListDelete(int i, ElemType& e) {
if (i < 1 || i > length) return ERROR;
e = elem[i - 1];
for (int j = i; j < length; j++) {
elem[j - 1] = elem[j];
}
length--;
return OK;
}
};
#endif
查出其中的技术问题,为何报错并提出修改方案
最新发布