Реализовать строку АДТ
String * subStr(String *s, int start, int end)возвращает срез s начиная с
начало индекса до и без учета конца индекса.
Выражение и ожидание вывода
ubStr(newStr("abcdefgh"), 2, 10) cdefgh subStr(newStr("abcdefgh"), 2, 6) cdef subStr(newStr("abcdefgh"), 0, 20) abcdefgh
Что я уже пробовал:
Но когда я запускаю свой код,
я получаю
bcdefgh bcde ""
#include <stdio.h> #include <stdlib.h> typedef struct ListNode{ int item; struct ListNode *next; }ListNode; typedef struct String { int size; ListNode head; } String; //put the new list head inside the first list ListNode *new_char_node(char c){ //create a new list node ListNode *n = (ListNode *)malloc(sizeof(ListNode)); n -> item = c; n -> next = NULL; return n; } String *newStr(char *c){ String *s = (String *)malloc(sizeof(String)); ListNode *p = &s->head; //access the head address by put the & while(*c){ p->next=new_char_node(*c++); p=p->next; s->size++; } return s; } String * subStr(String *s, int start, int end){ char buff[end-start+1]; ListNode *sp=&s->head; int i, j = 0; for (i = 0; i < end && sp != NULL; i++){ if(i>=start){ buff[j++] = sp->item; } // move to the start, read the item, put into the buffer sp = sp->next; } printf("%s\n", buff); return newStr(buff); } int main() { subStr(newStr("abcdefgh"), 2, 10); subStr(newStr("abcdefgh"), 2, 6); subStr(newStr("abcdefgh"), 0, 20); }
CPallini
Почему вы используете список понравившихся, где последовательности символов просто достаточно? Это обязательно?
JiaWei Lee
это упражнение по связанному списку. не мог понять, что пошло не так