基于数组的链表AList

快考试了,作为数据结构的复习,那么就把代码背打一遍好了,虽然代码很简单,但是把基础打牢固对以后的学习肯定是百益而无一害。

正好也练练C++模板和指针以及代码书写的模块化,否则天天写些算法程序,一写就一大坨,乱乱的,养成习惯就悲剧了... ...

看一点写一点,考试前补充完这篇博文就OK了

本来想写到一片文章中的,代码折叠显示,不过折叠后手机里就看不到了...还是分开写吧

《基于数组的链表》AList

 
  
#include < iostream >
using namespace std;

template
< class Elem >
class AList{
private :
int maxSize; // 链表所能存储的最大元素数量
int listSize; // 当前实际的元素数量
int fence; // 当前操作位置,俗称“栅栏”
Elem * listArray; // 存放链表元素的数组

public :

// 构造函数
AList( int size = 0 ){
maxSize
= size;
listSize
= fence = 0 ;
listArray
= new Elem[maxSize];
}
/* 找不到那个弯弯的符号,注掉先
//析构函数
ALsit(){
delete [] listArray;
}
*/
// 清除函数clear()
void clear(){
delete []listArray;
listSize
= fence = 0 ;
listArray
= new Elem[maxSize];
}

// 一系列的“栅栏”操作
void setStart(){ fence = 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于控制台的猜数字游戏,涵盖了一维数组、二维数组、字符串、指针、链表和文件等知识点,代码长度约为600行。希望对你有帮助。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define MAX_LEN 20 // 最大用户名长度 #define MAX_NUM 100 // 猜数字范围 // 用户信息结构体 struct UserInfo { char name[MAX_LEN]; // 用户名 int score; // 得分 }; // 链表节点结构体 struct ListNode { struct UserInfo data; // 数据域:用户信息 struct ListNode *next; // 指针域:下一个节点地址 }; // 函数声明 void printWelcome(); // 输出欢迎信息 int getUserChoice(); // 获取用户选择 void printRankingList(struct ListNode *head); // 输出排行榜 void saveRankingList(struct ListNode *head); // 保存排行榜到文件 void loadRankingList(struct ListNode **head); // 从文件加载排行榜 void addUserToRankingList(struct ListNode **head, struct UserInfo user); // 将用户添加到排行榜 void playGame(); // 玩游戏 int generateRandomNumber(); // 生成随机数 void updateScore(int *score, int guessCount); // 更新得分 int main() { printWelcome(); // 输出欢迎信息 int choice; // 用户选择 do { choice = getUserChoice(); // 获取用户选择 switch (choice) { case 1: // 开始游戏 playGame(); break; case 2: // 查看排行榜 struct ListNode *head; loadRankingList(&head); // 从文件加载排行榜 printRankingList(head); // 输出排行榜 break; case 3: // 退出游戏 printf("Goodbye!\n"); break; default: printf("Invalid choice! Please try again.\n"); break; } } while (choice != 3); return 0; } void printWelcome() { printf("Welcome to Guess Number game!\n"); } int getUserChoice() { printf("Please choose from the following options:\n"); printf("1. Play game\n"); printf("2. View ranking list\n"); printf("3. Quit game\n"); printf("Your choice: "); int choice; scanf("%d", &choice); getchar(); // 处理多余的换行符 return choice; } void printRankingList(struct ListNode *head) { printf("Ranking List:\n"); printf("%-10s %-10s\n", "Name", "Score"); int rank = 1; while (head != NULL && rank <= 10) { // 最多输出前10名 printf("%-10s %-10d\n", head->data.name, head->data.score); head = head->next; rank++; } } void saveRankingList(struct ListNode *head) { FILE *fp = fopen("rankinglist.dat", "wb"); // 以二进制写模式打开文件 if (fp == NULL) { printf("Failed to save ranking list to file!\n"); return; } while (head != NULL) { fwrite(&(head->data), sizeof(struct UserInfo), 1, fp); // 写入用户信息 head = head->next; } fclose(fp); // 关闭文件 } void loadRankingList(struct ListNode **head) { FILE *fp = fopen("rankinglist.dat", "rb"); // 以二进制读模式打开文件 if (fp == NULL) { *head = NULL; // 文件不存在或打开失败,链表为空 return; } struct UserInfo user; while (fread(&user, sizeof(struct UserInfo), 1, fp) == 1) { // 读取用户信息 addUserToRankingList(head, user); // 将用户添加到排行榜 } fclose(fp); // 关闭文件 } void addUserToRankingList(struct ListNode **head, struct UserInfo user) { struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); // 创建新节点 newNode->data = user; // 设置数据域 newNode->next = NULL; // 设置指针域 if (*head == NULL) { // 链表为空,直接插入 *head = newNode; } else { // 链表不为空,按得分从高到低插入 struct ListNode *p = *head; struct ListNode *prev = NULL; while (p != NULL && p->data.score > user.score) { prev = p; p = p->next; } if (prev == NULL) { // 插入到链表头 newNode->next = *head; *head = newNode; } else { // 插入到链表中间或尾部 newNode->next = p; prev->next = newNode; } } } void playGame() { printf("Please enter your name: "); char name[MAX_LEN]; fgets(name, MAX_LEN, stdin); // 读取用户输入的用户名 name[strlen(name) - 1] = '\0'; // 去掉换行符 int score = 0; // 初始化得分为0 int guessCount = 0; // 猜测次数 int randomNumber = generateRandomNumber(); // 生成随机数 printf("Guess a number between 1 and %d:\n", MAX_NUM); int guess; do { printf("Your guess: "); scanf("%d", &guess); getchar(); // 处理多余的换行符 guessCount++; if (guess < randomNumber) { printf("Too low! Try again.\n"); } else if (guess > randomNumber) { printf("Too high! Try again.\n"); } else { printf("Congratulations! You guessed the number in %d tries.\n", guessCount); updateScore(&score, guessCount); // 更新得分 printf("Your score: %d\n", score); } } while (guess != randomNumber); struct UserInfo user; strcpy(user.name, name); // 设置用户名 user.score = score; // 设置得分 struct ListNode *head; loadRankingList(&head); // 从文件加载排行榜 addUserToRankingList(&head, user); // 将用户添加到排行榜 saveRankingList(head); // 保存排行榜到文件 } int generateRandomNumber() { srand(time(NULL)); // 以当前时间为种子,生成随机数 return rand() % MAX_NUM + 1; // 返回1~MAX_NUM之间的随机数 } void updateScore(int *score, int guessCount) { if (guessCount == 1) { *score += 10; } else if (guessCount <= 3) { *score += 7; } else if (guessCount <= 6) { *score += 5; } else { *score += 2; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值