答疑解惑必采纳 2021-08-09 11:03 采纳率: 91.3%
浏览 19
已结题

c语言 填充(移动所有元素)字符列表,补全一下我的这个代码

在本练习中不应该创建新节点。您只需要在节点之间移动字符,您根本不应该使用malloc。
你将需要使用多个临时字符变量来完成这个练习!
img
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME_LENGTH 100

// Do not edit these structs. You may use them exactly as
// they are but you cannot make changes to them

// A node in a linked list of characters
struct character_node {
char data;
struct character_node *next;
};

// DECLARE ANY FUNCTIONS YOU WRITE HERE

// Given a list of characters, move each character
// along one, putting "pad_character" in the first place,
// and ignoring the last character.
//
// Given the list 'c' 'e' 'l' 'l' 'o'; calling left_pad
// once with pad_character = 'x' results in the list:
// 'x' 'c' 'e' 'l' 'l'. Calling it again with pad_character = 'e'
// results in the list: 'e' 'x' 'c' 'e' 'l'
//
// Note that you can't malloc, or modify the lists' nodes
// You can only move data around the list.
void pad_left(struct character_node *characters, char pad_character) {
// TODO: COMPLETE THIS FUNCTION
}

// These helper functions are only for this main, but
// may help you to both understand and test this exercise.
// They will not help you in the above exercise.
// You may use these functions for testing, but
// YOU CANNOT CHANGE THESE FUNCTIONS

// Convert a string to a linked list of nodes, one per character
struct character_node *string_to_characters(char *string) {
struct character_node *list_head = NULL;

int curr_char = strlen(string) - 1;
while (curr_char >= 0) {
    struct character_node *n = malloc(sizeof(struct character_node));
    n->data = string[curr_char];
    n->next = list_head;
    list_head = n;
    curr_char -= 1;
}

return list_head;

}

void print_characters(struct character_node *c) {
while (c != NULL) {
putchar(c->data);
c = c->next;
}
putchar('\n');
}

void free_characters(struct character_node *c) {
if (c == NULL) {
return;
}
free_characters(c->next);
free(c);
}

// This is a main function which could be used
// to test your pad_left function.
// It will not be marked.
// Only your pad_left function will be marked.
int main(int argc, char * argv[]) {
if (argc != 2) {
printf("Usage: %s a_string_to_pad\n", argv[0]);
return 0;
}
char *string = argv[1];
struct character_node *characters = string_to_characters(string);
int pad_character;
while ((pad_character = getchar()) != -1) {
if (pad_character == '\n') {
print_characters(characters);
} else {
pad_left(characters, pad_character);
}
}
free_characters(characters);

return 0;

}

// DEFINE ANY FUNCTIONS YOU WRITE HERE

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-08-09 11:30
    关注

    代码应该补在哪呢?

    void pad_left(struct character_node *characters, char pad_character) {
          struct character_node *p = characters;
          if(p == NULL)
              return;
          char c = pad_character;
          char t;
          while(p != NULL)    
          {
              t = p->data;
              p->data = c;
              c = t;
              p = p->next;
          }
          
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址