我正在尝试构建一个接收用户输入然后反转输入的程序。输出显示原始字符串和反转字符串。该程序编译但给我以下错误“0ÿÿÿ;原始链接列表分段错误(核心转储)”
这是我的代码:
struct node
{
int info;
struct node *next;
struct node *prev;
}node;
void reverse(struct node **head_1)
{
struct node *temp = NULL;
struct node *current = *head_1;
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head_1 = temp->prev;
}
void push(struct node** head_1, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->info = new_data;
new_node->prev = NULL;
new_node->next = (*head_1);
if((*head_1) != NULL)
(*head_1)->prev = new_node ;
(*head_1) = new_node;
}
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%s ", node->info);
node = node->next;
}
}
int main()
{
struct node* head = NULL;
char str[300], ch;
int i;
printf("enter ");
while((ch=getchar())!='\n');
{
str[i++]=ch;
str[i]='0';
i=0;
}
while (str[i]!='\0')
{
putchar(str[i++]);
push(&head, ch);
}
printf("\n Original ");
printList(head);
reverse(&head);
printf("\n Reversed ");
printList(head);
getchar();
}