数据结构-链表习题(C++)

程序设计题: 单链表实验
1.从键盘输入若干大于0的整数,用这些整数构造一个单链表.当用户输入小于等于0的值时创建链表结束并在终端打印输出这个链表。
2.在链表中查找某结点,如果能找到输出这个结点是第几个结点,如果找不到,输出:找不到此结点。
3.删除指定结点: 如果指定的被删除结点存在就删除它,然后打印:已经删除,如果不存在,输出信息: 链表中没有此结点,无法删除。
4.删除重复结点:如果链表中有重复的元素,删除重复的元素,使得所有值最多只出现一次,例如,如果链表中有3个结点的值是100,那么需要删除两个值为100的结点。只保留一个值为100的结点。删除重复结点后打印输出整个链表


1------------------------------------------------------
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

Node* createLinkedList() {
    Node* head = nullptr;
    Node* tail = nullptr;
    int input;
    while (true) {
        cin >> input;
        if (input <= 0) {
            break;
        }
        Node* newNode = new Node;
        newNode->data = input;
        newNode->next = nullptr;
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }
    return head;
}

void printLinkedList(Node* head) {
    Node* current = head;
    while (current != nullptr) {
        cout << current->data << " ";
        current = current->next;
    }
}

int main() {
    Node* head = createLinkedList();
    printLinkedList(head);
    return 0;
}

2------------------------------------------------------
int findNodePosition(Node* head, int value) {
    Node* current = head;
    int position = 1;
    while (current != nullptr) {
        if (current->data == value) {
            return position;
        }
        current = current->next;
        position++;
    }
    return -1; // Node not found
}
int main() {
    Node* head = createLinkedList();
    int valueToFind;
    cin >> valueToFind;
    int position = findNodePosition(head, valueToFind);
    if (position != -1) {
        cout << "The node is at position: " << position << endl;
    } else {
        cout << "Node not found" << endl;
    }
    return 0;
}
3------------------------------------------------------
int findNodePosition(Node* head, int value) {
    Node* current = head;
    int position = 1;
    while (current != nullptr) {
        if (current->data == value) {
            return position;
        }
        current = current->next;
        position++;
    }
    return -1; // Node not found
}

int main() {
    Node* head = createLinkedList();
    int valueToFind;
    cin >> valueToFind;
    int position = findNodePosition(head, valueToFind);
    if (position != -1) {
        cout << "The node is at position: " << position << endl;
    } else {
        cout << "Node not found" << endl;
    }
    return 0;
}
4------------------------------------------------------
Node* removeDuplicates(Node* head) {
    Node* current = head;
    while (current != nullptr) {
        Node* runner = current;
        while (runner->next != nullptr) {
            if (runner->next->data == current->data) {
                Node* temp = runner->next;
                runner->next = runner->next->next;
                delete temp;
            } else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
    return head;
}

int main() {
    Node* head = createLinkedList();
    head = removeDuplicates(head);
    printLinkedList(head);
    return 0;
}

相关推荐

  1. 数据结构-习题C++)

    2023-12-11 06:34:06       46 阅读
  2. 数据结构习题--反转

    2023-12-11 06:34:06       18 阅读
  3. 数据结构英文习题解析-第二章 List

    2023-12-11 06:34:06       26 阅读
  4. 数据结构习题--返回中点

    2023-12-11 06:34:06       10 阅读

最近更新

  1. Js- Math对象

    2023-12-11 06:34:06       0 阅读
  2. 基于Unity3D的Rokid AR Glass项目开发实战教程

    2023-12-11 06:34:06       0 阅读
  3. 每日一道算法题 求最小公倍数

    2023-12-11 06:34:06       0 阅读
  4. pycharm插件的安装

    2023-12-11 06:34:06       0 阅读
  5. 配置管理新纪元:Eureka引领分布式服务配置潮流

    2023-12-11 06:34:06       0 阅读
  6. cpp http server/client

    2023-12-11 06:34:06       1 阅读
  7. Html利用Vue动态加载单文件页面【httpVueLoader】

    2023-12-11 06:34:06       1 阅读
  8. linux:命令执行过程【图表】

    2023-12-11 06:34:06       1 阅读

热门阅读

  1. vulnhub靶机Phineas

    2023-12-11 06:34:06       46 阅读
  2. 机器学习基础入门

    2023-12-11 06:34:06       39 阅读
  3. 信息安全、网络安全和数据安全的相互关系

    2023-12-11 06:34:06       48 阅读
  4. 【前端设计模式】之迭代器模式

    2023-12-11 06:34:06       38 阅读
  5. 主流MQ [Kafka、RabbitMQ、ZeroMQ、RocketMQ 和 ActiveMQ]

    2023-12-11 06:34:06       42 阅读
  6. PHP基础 - 运算符

    2023-12-11 06:34:06       28 阅读
  7. android 13.0 Settings去掉二级三级菜单搜索功能

    2023-12-11 06:34:06       37 阅读