问题 A 实验11_4_初识链表

问题 A 实验11_4_初识链表

题目描述
已知一个正整数序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后统计这个序列中元素的最大值与最小值,计算序列全部元素之和。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。
输入
一个正整数序列,正整数序列元素的个数未知,但以输入“-1”结束,输入“-1”前至少输入一个正整数。序列中的元素范围在1—999999999之间。
输出
三个正整数,即最大值、最小值、所有元素之和。
数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。
请注意输入输出格式。

样例输入 Copy

1 4 99 21 50 61 32 4 -1

样例输出 Copy

The maximum,minmum and the total are:99 1 272
/* creat the linked list and get the max, min and sum
@author: CangCheng
@date: 13/3/2021
*/

#include<stdio.h>
#include <stdlib.h>

struct node {
    int data;
    int *nextPtr;
};

struct node *creatLinkedList(struct node *headPtr);

void delLinkedList(struct node *headPtr);

int getMax(struct node *headPtr);

int getMin(struct node *headPtr);

int getSum(struct node *headPtr);

int main() {
    struct node *headPtr = NULL;
    int max, min, sum;
    headPtr = creatLinkedList(headPtr);
    max = getMax(headPtr);
    min = getMin(headPtr);
    sum = getSum(headPtr);
    printf("The maximum,minmum and the total are:%d %d %d", max, min, sum);
    delLinkedList(headPtr);
    return 0;
}

//创建链表
struct node *creatLinkedList(struct node *headPtr) {
    struct node *newPtr, *currentPtr;
    int num;
    scanf("%d", &num);
    while (num != -1) {
        newPtr = malloc(sizeof(struct node));
        newPtr->data = num;
        newPtr->nextPtr = NULL;
        if (headPtr == NULL) {
            currentPtr = newPtr;
            headPtr = currentPtr;
        } else {
            currentPtr->nextPtr = newPtr;
            currentPtr = newPtr;
        }
        scanf("%d", &num);
    }
    return headPtr;
}

//删除链表
void delLinkedList(struct node *headPtr) {
    struct node *thisPtr, *thatPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        thatPtr = thisPtr->nextPtr;
        free(thisPtr);
        thisPtr = thatPtr;
    }
}

//得到最大值
int getMax(struct node *headPtr) {
    struct node *thisPtr;
    int max;
    thisPtr = headPtr;
    max = headPtr->data;
    while (thisPtr != NULL) {
        if (thisPtr->data > max) {
            max = thisPtr->data;
        }
        thisPtr = thisPtr->nextPtr;
    }
    return max;
}

//得到最小值
int getMin(struct node *headPtr) {
    struct node *thisPtr;
    int min;
    thisPtr = headPtr;
    min = headPtr->data;
    while (thisPtr != NULL) {
        if (thisPtr->data < min) {
            min = thisPtr->data;
        }
        thisPtr = thisPtr->nextPtr;
    }
    return min;
}

//得到和
int getSum(struct node *headPtr) {
    struct node *thisPtr;
    int sum;
    thisPtr = headPtr;
    sum = 0;
    while (thisPtr != NULL) {
        sum = sum + thisPtr->data;
        thisPtr = thisPtr->nextPtr;
    }
    return sum;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这道题目是要求我们实现链表的基本操作,包括创建链表、插入节点、删除节点、遍历链表等。以下是一个简单的链表实现代码示例: ```c++ #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 创建链表 ListNode* createList(int nums[], int n) { if (n == 0) return NULL; ListNode *head = new ListNode(nums[0]); ListNode *cur = head; for (int i = 1; i < n; i++) { cur->next = new ListNode(nums[i]); cur = cur->next; } return head; } // 插入节点 void insertNode(ListNode *&head, int pos, int val) { if (pos < 0) return; if (pos == 0) { ListNode *newNode = new ListNode(val); newNode->next = head; head = newNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur) { ListNode *newNode = new ListNode(val); newNode->next = cur->next; cur->next = newNode; } } // 删除节点 void deleteNode(ListNode *&head, int pos) { if (pos < 0) return; if (pos == 0) { ListNode *delNode = head; head = head->next; delete delNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur && cur->next) { ListNode *delNode = cur->next; cur->next = delNode->next; delete delNode; } } // 遍历链表 void traverseList(ListNode *head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { int nums[] = {1, 2, 3, 4, 5}; int n = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, n); traverseList(head); insertNode(head, 2, 6); traverseList(head); deleteNode(head, 3); traverseList(head); return 0; } ``` 在上面的代码中,我们定义了一个 `ListNode` 结构体作为链表节点,包括节点值 `val` 和指向下一个节点的指针 `next`。同时,我们实现了创建链表、插入节点、删除节点和遍历链表等基本操作。在使用链表时,我们可以先通过 `createList` 函数创建一个链表,然后对链表进行插入、删除和遍历操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值