| 
             | 
          
             #2 
            rjsp2025-03-31 12:39
                                      大佬们有没有知道如何在控制台程序控制输出,就是会停顿一下。 听不懂!但你的代码写得像天书,我忍不住修改一下  程序代码:#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编辑过]  | 
        
    以下是我写的代码,关于注释中的代码,大佬们有没有知道如何在控制台程序控制输出,就是会停顿一下。
哦,关于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;  
程序代码: