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

程序设计题: 单链表实验

1.从键盘输入若干大于0的整数,用这些整数构造一个单链表.当用户输入小于等于0的值时创建链表结束并在终端打印输出这个链表。

2.在链表中查找某结点,如果能找到输出这个结点是第几个结点,如果找不到,输出:找不到此结点。

3.删除指定结点: 如果指定的被删除结点存在就删除它,然后打印:已经删除,如果不存在,输出信息: 链表中没有此结点,无法删除。

4.删除重复结点:如果链表中有重复的元素,删除重复的元素,使得所有值最多只出现一次,例如,如果链表中有3个结点的值是100,那么需要删除两个值为100的结点。只保留一个值为100的结点。删除重复结点后打印输出整个链表

C++:

cpp 复制代码
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;
}

C:

cs 复制代码
#include <stdio.h>
#include <stdlib.h>

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

struct Node* createLinkedList() {
    struct Node* head = NULL;
    struct Node* tail = NULL;
    int input;
    while (1) {
        scanf("%d", &input);
        if (input <= 0) {
            break;
        }
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = input;
        newNode->next = NULL;
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }
    return head;
}

void printLinkedList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
}

int findNodePosition(struct Node* head, int value) {
    struct Node* current = head;
    int position = 1;
    while (current != NULL) {
        if (current->data == value) {
            return position;
        }
        current = current->next;
        position++;
    }
    return -1; // Node not found
}

struct Node* removeDuplicates(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        struct Node* runner = current;
        while (runner->next != NULL) {
            if (runner->next->data == current->data) {
                struct Node* temp = runner->next;
                runner->next = runner->next->next;
                free(temp);
            } else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
    return head;
}

int main() {
    struct Node* head = createLinkedList();
    printLinkedList(head);
    int valueToFind;
    scanf("%d", &valueToFind);
    int position = findNodePosition(head, valueToFind);
    if (position != -1) {
        printf("The node is at position: %d\n", position);
    } else {
        printf("Node not found\n");
    }
    head = removeDuplicates(head);
    printLinkedList(head);
    return 0;
}
相关推荐
ALex_zry8 分钟前
C++20/23标准对进程间共享信息的优化:从传统IPC到现代C++的演进
开发语言·c++·c++20
郝学胜-神的一滴24 分钟前
深入解析Linux的`pthread_create`函数:从原理到实践
linux·服务器·c++·程序人生
小年糕是糕手27 分钟前
【C/C++刷题集】string类(一)
开发语言·数据结构·c++·算法·leetcode
暗然而日章29 分钟前
C++基础:Stanford CS106L学习笔记 12 运算符重载
c++·笔记·学习
ToddyBear35 分钟前
从字符游戏到 CPU 指令集:一道算法题背后的深度思维跃迁
数据结构·算法
JAVA+C语言41 分钟前
C++ 继承与派生
开发语言·c++
Andyshengwx1 小时前
图论 最小生成树 MST问题
c++·算法·图论
賬號封禁中miu1 小时前
图论之最小生成树
java·数据结构·算法·图论
闻缺陷则喜何志丹1 小时前
【图论 拓扑排序 贪心 临项交换】P5603 小 C 与桌游 题解|普及+
c++·算法·图论·贪心·拓扑排序·洛谷·临项交换
闻缺陷则喜何志丹1 小时前
【图论 BFS染色 并集查找 】P3663 [USACO17FEB] Why Did the Cow Cross the Road III S|普及+
c++·算法·图论·染色法·宽度优先·并集查找