数据结构链表理解练习
游戏设计:
随机输出一组N个2位数,第一步:删除玩家输入位置k处的一个数,第二步,输入一个x位置将上一步删除的数插入x处;
当所有数,从小到大排列则游戏结束,并输出总
方案一:(数组方式实现)
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> using namespace std; const int N = 18; //有效输入域检测 bool chec(int max, int k) { if (k > 0 && k <= max) { return true; } cout << " the number must 1 to " << max <<":"; return false; } //输出有效数据 void put(const int (&a)[N], int n) { for (int i = 1; i <= n; i++) { cout << setw(3) << i; } cout << endl; for (int i = 0; i < n; i++) { cout << setw(3) << a[i]; } } //删除 int del(int (&a)[N], int k) { int temp = a[k - 1]; for (int i = k; i < N; i++) { a[i - 1] = a[i]; } a[N - 1] = 0; return temp; } //插入 void ins(int (&a)[N], int k, int x) { for (int i = N - 1; i >= k; i--) { a[i] = a[i - 1]; } a[k - 1] = x; } //比较 bool match(const int (&a)[N]) { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (a[i] > a[j]) return false; } } return true; } int main() { int ar[N]; int k = 0; bool Gameover = false; srand(time(NULL)); for (int i = 0; i < N; i++) { ar[i] = 10 + rand() % 89; } put(ar, N); while (!Gameover) { static int sub = 0; int key1 = 0, key2 = 0; cout << " printf delate postion:"; do { cin >> key1; } while (!chec(N, key1)); int temp = del(ar, key1); put(ar, N - 1); cout << " printf insert postion:"; do { cin >> key2; } while (!chec(N, key2)); ins(ar, key2, temp); put(ar, N); sub++; if (match(ar)) { cout << endl << "---------game over" << "------tall item: " << sub * 2; Gameover = true; } } return 0; }
方案二:(链表存储)
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> using namespace std; const int N = 15; typedef struct Node *List; struct Node { int data; List next; }; //检测输入 bool chec(int max, int k) { if (k > 0 && k <= max) { return true; } cout << " the number must 1 to " << max << ":"; return false; } //创建链表 List createList(int n) { srand(time(NULL)); List head = NULL; for (int i = 0; i < n; i++) { List temp = new Node; temp->data = 10 + rand() % 89; temp->next = head; head = temp; } return head; } //输出链表 void printList(List t, int L) { List temp = t; for (int i = 0; i < L; i++) { cout << setw(3) << temp->data; temp = temp->next; } } //删除 List del(List &T, int k) { if (1 == k) { List temp = T; T = T->next; temp->next = NULL; return temp; } else { int i = 1; List temp = T; List rTemp = NULL; while (i != k - 1) { i++; temp = temp->next; } rTemp = temp->next; temp->next = rTemp->next; rTemp->next = NULL; return rTemp; } } //插入 void ins(List &T, int k, List &P) { if (1 == k) { P->next = T; T = P; } else { int i = 1; List temp = T; while (i != k - 1) { i++; temp = temp->next; } P->next = temp->next; temp->next = P; } } //比较 bool match(List &T, int n) { List temp = T; int i = 1; while (i != n) { i++; if (temp->data > temp->next->data) { return false; } else { temp = temp->next; } } return true; } //编号输出 void put(int n) { for (int i = 1; i <= n; i++) { cout << setw(3) << i; } cout << endl; } int main() { int k = 0; bool Gameover = false; put(N); List L1 = createList(N); printList(L1, N); while (!Gameover) { static int sub = 0; int key1 = 0, key2 = 0; cout << " printf delate postion:"; do { cin >> key1; } while (!chec(N, key1)); put(N - 1); List temp = del(L1, key1); printList(L1, N - 1); cout << " printf insert postion:"; do { cin >> key2; } while (!chec(N, key2)); put(N); ins(L1, key2, temp); printList(L1, N); sub++; if (match(L1, N)) { cout << endl << "---------game over" << "------tall item: " << sub * 2; Gameover = true; } } return 0; }
通用链表的设计:
//节点定义
typedef Node* List;
strcut Node
{
ElementType data;
List next;
}
线性表常规操作:
List MakeEmpty(); //初始化空表
ElementType FindKth(int K, List L); //根据位序k返回相应元素;
int Find(ElementType x, List L); //根据元素x,返回在表中对应的位置;
int length(List L); //返回线性表长度;
void delete(int k, List L); //删除指定位置元素;
void insert(ElementType x, int k, List L); //在指定位置插入指定元素;
c语言实现
#pragma GCC diagnostic error "-std=c++11" #include <iostream> using namespace std; template <class T> struct LNode { T data; LNode<T> *next; }; template <typename T> using List = LNode<T> *; //创建空链表 template <typename T> List<T> createList() { List<T> temp = new LNode<T>; return temp; } //求表长 template <typename T> int length(List<T> n) { List<T> temp = n; int i = 0; while (temp) { i++; temp = temp->next; } return i; } //查找特定元素 template <typename T> List<T> findKth(int k, List<T> n) { if (k < 1 || k > length(n)) { cout << "erro findKth K"; return NULL; } else { List<T> temp = n; int i = 1; while (k != i && n != NULL)
{ i++; temp = temp->next; } return temp; } } //控值查找 template <typename T> int find(T x, List<T> n) { List<T> temp = n; int i = 1; while (temp != NULL && temp->data != x)
{ i++; temp = temp->next; } if (i > length(n)) { cout << "erro find X"; return 0; } else { return i; } } //插入节点 template <typename T> void insert(T x, int k, List<T> &n) { if (k < 1 || k > length(n)) { cout << "erro insert K"; return; } else { List<T> temp = createList<T>(); temp->data = x; temp->next = NULL; if (1 == k) { n->next = temp; } else { List<T> tempK_1 = findKth(k - 1, n); temp->next = tempK_1->next; tempK_1->next = temp; } } } //删除节点 template <typename T> void delet(int k, List<T> &n) { if (1 == k) { n = n->next; } else if (k != length(n)) { List<T> temp = findKth(k - 1, n); temp->next = temp->next->next; } else { List<T> temp = findKth(k - 1, n); temp->next = NULL; } } //输出链表值 template <typename T> void printList(List<T> n) { int lth = length(n); cout << "length :" << lth << endl; List<T> temp = n; for (int i = 0; i < lth; i++) { cout << temp->data << " "; temp = temp->next; } cout << endl; } int main() { List<int> list = createList<int>(); list->data = 11; list->next = NULL; printList<int>(list); //插入新项 insert<int>(12, 1, list); printList<int>(list); //插入新项 insert<int>(13, 2, list); printList<int>(list); //插入新项 insert<int>(14, 2, list); printList<int>(list); //删除新项 delet<int>(4, list); printList<int>(list); //删除新项 delet<int>(2, list); printList<int>(list); //删除新项 delet<int>(1, list); printList<int>(list); return 0; }
c++实现