| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 204 人关注过本帖
标题:关于链表的疑惑
只看楼主 加入收藏
sheep_
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2025-3-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
关于链表的疑惑
以下是我写的代码,关于注释中的代码,大佬们有没有知道如何在控制台程序控制输出,就是会停顿一下。
哦,关于sll_reverse()函数有没有更好的办法,我想不出来。
#include <stdlib.h>
#include <stdio.h>
#include "singly_linked_list.h"
//struct  NODE * sll_reverse(struct NODE * first);
int sll_remove(Node ** rootp, int value);
int dll_insert(Node ** rootp, int value);
Node * sll_reverse(Node *first);
int main(){
    int num;
    Node *first = (Node *)malloc(sizeof(Node));
    if(first == NULL){
        perror("NO Buffle: ");
        return 1;
    }
    first->value = 3;
    first->link = NULL;
    Node *rootp = first;
    while (scanf("%d" , &num) != EOF){
        dll_insert(&rootp, num);
    }
    Node * ptr = rootp;
    printf("\n");
    while(ptr != NULL){
        printf(" %d", ptr->value);
        ptr = ptr->link;
    }
   /* getchar();getchar();
    printf("Input a single number you want to remove: ");
    getchar();
    while(scanf("%d", &num) != EOF){
        sll_remove(&rootp, num);
    }
    ptr = rootp;
    while(ptr != NULL){
        printf(" %d", ptr->value);
        ptr = ptr->link;
    }*/
    rootp = sll_reverse(rootp);
    printf("\n");
    ptr = rootp;
    while(ptr != NULL){
        printf(" %d", ptr->value);
        ptr = ptr->link;
    }
    ptr = rootp;
    while(ptr != NULL){
        Node * temp=ptr;
        ptr = ptr->link;
        free(temp);
    }
    return 0;
}
int dll_insert(Node ** rootp, int value){
    Node *previous = NULL;
    Node *current = *rootp;
    while(current != NULL && current->value < value){
        previous = current;
        current = current->link;
    }
    if (current != NULL && current-> value == value)
        return 0;
    Node *new = (Node *)malloc(sizeof(Node));
    if (new == NULL){
        perror("NO buffle : ");
        exit(EXIT_FAILURE);
    }
    new->value = value;
    new->link = current;
    if(previous != NULL){
        previous->link  = new;
    }
    else{
        *rootp = new;
    }
    return 0;
}
int sll_remove(Node ** rootp,int value){
    Node *previous = NULL;
    Node *current = *rootp;
   
    while(current != NULL && current->value != value){
        previous = current;
        current = current->link;
    }
    if(current != NULL){
        Node * temp = current;
        current = current->link;
        free(temp);
        if(previous != NULL){
            return 0;
        }
        else {
            *rootp = current;
            return 0;
        }
    }
    else {
        printf("No this module. ");
        return 1;
    }
}
      

Node * sll_reverse(Node * first){
    int i = 0;
    Node *temp = first;
    while(temp != NULL){
        temp = temp->link;
        i++;
    }
    if( i == 0){
        printf("None Node.");
        return first;
    }
    Node *array[i];
    temp = first;
    for(int j = 0; j< i;j++){
        array[j] = temp;
        temp = temp->link;
    }
    Node *new =  array[i-1];
    for(int k = i-1;k >= 0;k--){
        temp = array[k];
        if(k != 0){
            temp->link = array[k-1];
        }
        else{
            temp->link = NULL;
        }
    }
    return new;
}
头文件single_link_list.h
typedef struct NODE {
    int value;
    struct NODE * link;
}Node;
搜索更多相关主题的帖子: NULL link value NODE current 
2025-03-30 10:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9051
专家分:54255
注 册:2011-1-18
收藏
得分:20 
大佬们有没有知道如何在控制台程序控制输出,就是会停顿一下。
听不懂!
但你的代码写得像天书,我忍不住修改一下

程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct NODE {
    int value;
    struct NODE* link;
} Node;

void sll_insert( Node** root, int value );
void sll_remove( Node** root, int value );
void sll_reverse( Node** root );
void sll_free( Node** root );
void sll_outputln( Node* const * root );

int main( void )
{
    // 创建
    Node* root = NULL;

    // 插入
    printf( "%s", "Input a single number you want to insert: " );
    for( int num; scanf("%d",&num)==1; )
    {
        sll_insert( &root, num );
        sll_outputln( &root );
    }

    // 删除
    printf( "%s", "Input a single number you want to remove: " );
    for( int num; scanf("%d",&num)==1; )
    {
        sll_remove( &root, num );
        sll_outputln( &root );
    }

    // 反向
    printf( "%s", "Reversed sequence: " );
    sll_reverse( &root );
    sll_outputln( &root );

    // 销毁
    sll_free( &root );
    sll_outputln( &root );
}

void sll_insert( Node** root, int value )
{
    Node** p;
    for( p=root; *p && (*p)->value<value; p=&(*p)->link );

    Node* q = malloc( sizeof(Node) );
    if( q )
    {
        q->value = value;
        q->link = *p;
        *p = q;
    }
}

void sll_remove( Node** root, int value )
{
    Node** p;
    for( p=root; *p && (*p)->value!=value; p=&(*p)->link );

    if( *p )
    {
        Node* temp = *p;
        *p = (*p)->link;
        free( temp );
    }
}

void sll_reverse( Node** root )
{
    Node* prev = NULL;
    for( Node** p=root; *p;  )
    {
        Node* next = (*p)->link;
        (*p)->link = prev;
        prev = *p;
        *p = next;
    }
    *root = prev;
}

void sll_free( Node** root )
{
    for( Node** p=root; *p;  )
    {
        Node* next = (*p)->link;
        free( *p );
        *p = next;
    }
}

void sll_outputln( Node* const * root )
{
    putchar( '{' );
    for( const Node* p=*root; p; p=p->link )
        printf( ", %d"+(p==*root), p->value );
    puts( " }" );
}

测试:
程序代码:
Input a single number you want to insert: 2
{ 2 }
4
{ 2, 4 }
6
{ 2, 4, 6 }
5
{ 2, 4, 5, 6 }
3
{ 2, 3, 4, 5, 6 }
1
{ 1, 2, 3, 4, 5, 6 }
^Z
^Z
^Z
Input a single number you want to remove: 4
{ 1, 2, 3, 5, 6 }
1
{ 2, 3, 5, 6 }
6
{ 2, 3, 5 }
^Z
^Z
^Z
Reversed sequence: { 5, 3, 2 }
{ }



[此贴子已经被作者于2025-3-31 12:59编辑过]

2025-03-31 12:39
wtyj112
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2007-5-9
收藏
得分:0 
#include <stdlib.h>
#include <stdio.h>

typedef struct NODE {
    int value;
    struct NODE *link;
} Node;

int sll_remove(Node **rootp, int value);
int dll_insert(Node **rootp, int value);
Node *sll_reverse(Node *first);

void print_list(Node *rootp) {
    Node *ptr = rootp;
    printf("Current List: ");
    while (ptr != NULL) {
        printf("%d ", ptr->value);
        ptr = ptr->link;
    }
    printf("\n");
}

int main() {
    int choice, num;
    Node *first = (Node *)malloc(sizeof(Node));
    if (first == NULL) {
        perror("NO Buffle: ");
        return 1;
    }
    first->value = 3;
    first->link = NULL;
    Node *rootp = first;

    while (1) {
        // 显示菜单
        printf("\nMenu:\n");
        printf("1. Insert a number\n");
        printf("2. Remove a number\n");
        printf("3. Print the list\n");
        printf("4. Reverse the list\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        
        // 读取用户选择
        if (scanf("%d", &choice) != 1) {
            printf("Invalid input. Please enter a number.\n");
            while (getchar() != '\n'); // 清空输入缓冲区
            continue;
        }

        switch (choice) {
            case 1: // 插入数字
                printf("Enter a number to insert: ");
                if (scanf("%d", &num) != 1) {
                    printf("Invalid input. Please enter a number.\n");
                    while (getchar() != '\n'); // 清空输入缓冲区
                    continue;
                }
                dll_insert(&rootp, num);
                printf("Number %d inserted.\n", num);
                break;

            case 2: // 删除数字
                printf("Enter a number to remove: ");
                if (scanf("%d", &num) != 1) {
                    printf("Invalid input. Please enter a number.\n");
                    while (getchar() != '\n'); // 清空输入缓冲区
                    continue;
                }
                if (sll_remove(&rootp, num) == 0) {
                    printf("Number %d removed.\n", num);
                } else {
                    printf("Number %d not found in the list.\n", num);
                }
                break;

            case 3: // 打印链表
                print_list(rootp);
                break;

            case 4: // 反转链表
                rootp = sll_reverse(rootp);
                printf("List reversed.\n");
                print_list(rootp);
                break;

            case 5: // 退出程序
                printf("Exiting program...\n");
                Node *ptr = rootp;
                while (ptr != NULL) {
                    Node *temp = ptr;
                    ptr = ptr->link;
                    free(temp);
                }
                return 0;

            default: // 无效选择
                printf("Invalid choice. Please select a valid option.\n");
                break;
        }
    }
}

int dll_insert(Node **rootp, int value) {
    Node *previous = NULL;
    Node *current = *rootp;
    while (current != NULL && current->value < value) {
        previous = current;
        current = current->link;
    }
    if (current != NULL && current->value == value)
        return 0;
    Node *new_node = (Node *)malloc(sizeof(Node));
    if (new_node == NULL) {
        perror("NO buffle : ");
        exit(EXIT_FAILURE);
    }
    new_node->value = value;
    new_node->link = current;
    if (previous != NULL) {
        previous->link = new_node;
    } else {
        *rootp = new_node;
    }
    return 0;
}

int sll_remove(Node **rootp, int value) {
    Node *previous = NULL;
    Node *current = *rootp;

    while (current != NULL && current->value != value) {
        previous = current;
        current = current->link;
    }
    if (current != NULL) {
        Node *temp = current;
        current = current->link;
        free(temp);
        if (previous != NULL) {
            previous->link = current;
        } else {
            *rootp = current;
        }
        return 0;
    } else {
        return 1;
    }
}

Node *sll_reverse(Node *first) {
    Node *prev = NULL;  // 用于存储当前节点的前一个节点
    Node *current = first;  // 当前节点
    Node *next = NULL;  // 用于存储当前节点的下一个节点

    while (current != NULL) {
        next = current->link;  // 保存当前节点的下一个节点
        current->link = prev;  // 将当前节点的链接指向前一个节点
        prev = current;        // 更新前一个节点为当前节点
        current = next;        // 移动到下一个节点
    }

    return prev;  // 返回新的头节点
}

没有用过ai工具么,现在的ai助手很适合做新手导师, 2分钟就优化好了, 链表逆转是很老的算法题了。

计算机之路是痛苦并快乐着的!!
前天 09:49
快速回复:关于链表的疑惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073631 second(s), 8 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved