| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 88 人关注过本帖
标题:关于链表的疑惑
只看楼主 加入收藏
sheep_
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2025-3-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
关于链表的疑惑
以下是我写的代码,关于注释中的代码,大佬们有没有知道如何在控制台程序控制输出,就是会停顿一下。
哦,关于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;
2025-03-30 10:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9047
专家分:54175
注 册: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
快速回复:关于链表的疑惑
数据加载中...
 
   



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

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