单链表创建销毁尾插操作
This commit is contained in:
parent
8848d82713
commit
b60cc2f40d
@ -23,6 +23,7 @@
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
102A36C1298C114B0037F7B0 /* LinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LinkList.h; sourceTree = "<group>"; };
|
||||
108A953A29878C0D005C28B4 /* DataStructure */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DataStructure; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
108A953D29878C0D005C28B4 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
|
||||
108A954429878C66005C28B4 /* DoubleLinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoubleLinkList.h; sourceTree = "<group>"; };
|
||||
@ -63,6 +64,7 @@
|
||||
children = (
|
||||
108A953D29878C0D005C28B4 /* main.cpp */,
|
||||
109F279E2988BD3E00A3EF26 /* StaticSequenceList.h */,
|
||||
102A36C1298C114B0037F7B0 /* LinkList.h */,
|
||||
108A9547298794DA005C28B4 /* CycleDoubleLinkList.h */,
|
||||
108A9546298792B3005C28B4 /* CycleLinkList.h */,
|
||||
108A954429878C66005C28B4 /* DoubleLinkList.h */,
|
||||
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Bucket
|
||||
uuid = "ACC9A79D-D37F-4CE2-8FBA-491472288DF0"
|
||||
type = "1"
|
||||
version = "2.0">
|
||||
</Bucket>
|
@ -1,18 +1,97 @@
|
||||
//
|
||||
// LinkList.h
|
||||
// DataStructure
|
||||
//
|
||||
// Created by Noth Amor on 2023/1/30.
|
||||
// 单链表
|
||||
// Created by Noth Amor on 2023/2/2.
|
||||
//
|
||||
|
||||
#ifndef LinkList_h
|
||||
#define LinkList_h
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct LNode {
|
||||
int data;
|
||||
struct LNode *next;
|
||||
}LNode, *LinkList;
|
||||
|
||||
// 单链表初始化
|
||||
bool InitLinkList(LinkList &L) {
|
||||
L = (LNode *) malloc(sizeof(LNode));
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 单链表判空
|
||||
bool LinkListEmpty(LinkList L) {
|
||||
return L->next == NULL;
|
||||
}
|
||||
|
||||
// 销毁单链表
|
||||
void DestroyLinkList(LinkList &L) {
|
||||
free(L);
|
||||
}
|
||||
|
||||
// 遍历输出单链表
|
||||
void PrintLinkList(LinkList L) {
|
||||
if (L->next == NULL) {
|
||||
std::cout << "当前链表内无数据" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
LNode *t = L->next;
|
||||
while (t != NULL) {
|
||||
std::cout << t->data;
|
||||
t = t->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// 单链表尾插法
|
||||
void LinkListInsert(LinkList &L, int x) {
|
||||
// 如果L只有头节点,没有后继节点
|
||||
if (L->next == NULL) {
|
||||
LNode *s = (LNode *) malloc(sizeof(LNode));
|
||||
s->data = x;
|
||||
s->next = NULL;
|
||||
L->next = s;
|
||||
return;
|
||||
}
|
||||
|
||||
// L有后继节点
|
||||
LNode *t = L->next;
|
||||
while (t != NULL) {
|
||||
if (t->next == NULL) {
|
||||
LNode *s = (LNode *) malloc(sizeof(LNode));
|
||||
s->data = x;
|
||||
s->next = NULL;
|
||||
t->next = s;
|
||||
return;
|
||||
}
|
||||
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void LinkListTest() {
|
||||
LinkList L;
|
||||
InitLinkList(L);
|
||||
|
||||
LinkListInsert(L, 1);
|
||||
LinkListInsert(L, 3);
|
||||
LinkListInsert(L, 5);
|
||||
LinkListInsert(L, 7);
|
||||
|
||||
//DestroyLinkList(L);
|
||||
|
||||
PrintLinkList(L);
|
||||
|
||||
//std::cout << "link list status(should be 1): " << initStatus << std::endl;
|
||||
}
|
||||
|
||||
#endif /* LinkList_h */
|
||||
|
@ -238,7 +238,46 @@ SqList MergeTwoOrderlySequenceList(SqList L1, SqList L2) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// 已知在一维数组A[m+n]中依次存放两个线性表m和n,编写一个函数,将数组中两个顺序表的位置互换,即将n放在m的前面。
|
||||
void Reverse(int A[], int middle, int arraySize) {
|
||||
int temp = 0;
|
||||
for (int i = 0; i < arraySize / 2; i++) {
|
||||
temp = A[i];
|
||||
A[i] = A[arraySize - i];
|
||||
A[arraySize - i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// 线性表中的元素递增有序且按顺序存储于计算机内,要求设计一个算法,完成用最少时间在表中查找数值为x的元素,若找到,则将其与后继元素位置相交换,若找不到,则将其插入表中并使表中元素仍递增有序。
|
||||
void SearchExchangeInsert(SqList &L, int x) {
|
||||
if (L.size == 0 && L.size + 1 < L.length) {
|
||||
std::cout << "顺序表尚未初始化或顺序表空间不足" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int temp = 0;
|
||||
for (int i = 0; i < L.size; i++) {
|
||||
if (L.data[i] == x) {
|
||||
temp = L.data[i];
|
||||
L.data[i] = L.data[i + 1];
|
||||
L.data[i + 1] = temp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < L.size; i++) {
|
||||
if (x > L.data[i] && x < L.data[i + 1]) {
|
||||
for (int j = L.size + 1; j > i + 1; j--) {
|
||||
temp = L.data[j];
|
||||
L.data[j] = L.data[j - 1];
|
||||
L.data[j - 1] = temp;
|
||||
}
|
||||
L.data[i + 1] = x;
|
||||
L.size++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 静态顺序表测试
|
||||
void StaticSequenceListTest() {
|
||||
@ -280,24 +319,15 @@ void StaticSequenceListTest() {
|
||||
L2.data[3] = 8;
|
||||
L2.size = 4;
|
||||
|
||||
for (int i = 0; i < L1.size; i++) {
|
||||
std::cout << L1.data[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
for (int i = 0; i < L2.size; i++) {
|
||||
std::cout << L2.data[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
//PrintList(L);
|
||||
//ReverseSequenceListElement(L);
|
||||
//DeleteSpecifiedElementInSequenceList(L, 4);
|
||||
//DeleteElementBetweenSAndT(L, 2, 4);
|
||||
//DeleteElementBetweenSAndTWithItself(L, 2, 4);
|
||||
//DropDuplicateElementInSequenceList(L);
|
||||
SqList result = MergeTwoOrderlySequenceList(L1, L2);
|
||||
PrintList(result);
|
||||
//SqList result = MergeTwoOrderlySequenceList(L1, L2);
|
||||
SearchExchangeInsert(L1, 2);
|
||||
PrintList(L1);
|
||||
|
||||
DestroyList(L);
|
||||
}
|
||||
|
@ -1,17 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include "StaticSequenceList.h"
|
||||
#include "LinkList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 静态链表
|
||||
typedef struct {
|
||||
int data;
|
||||
int next;
|
||||
} SLinkList[10];
|
||||
|
||||
int main() {
|
||||
StaticSequenceListTest();
|
||||
|
||||
LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user