C语言单链表改成双链表

把下面单链表改成双链表,并将输入的字符串方向显示(使用wintc编译):
/* HELLO.C -- Hello, world */

#include "stdio.h"
struct node /*点链表的数据结构*/
{
char ch;
struct node *next;
};
main()
{
struct node *head; /*head是头指针,指向点链表第一个节点*/
struct node *newhead; /*用于指向新申请的节点*/
struct node *prenode; /*指向新节点的前一节点*/
char inputch;
int k=0;
while(1==1)
{
inputch=gech();
if(inputch==13)break;
newnode=(struct node)malloc(sizeof(struct node));
newnode->ch=
if(k==0) {head=newnode;prenode=newnode;}
else {prenode->next=newnode;}
prenode=newnode; /*使当前的新节点变成下一次的前一节点*/
k++;
}
newnode=head;
while(newnode!=null)
{
printf("%c",newnode->ch)};
nownode=newnode->next;
}
newnode=head->next;
while(newnode!=null)
{
free(head);
head=newnode;
newnode=head->next;
}
getch();
}

请高手帮忙,谢谢

#include "stdio.h"
#include <conio.h>
#include <stdlib.h>

struct node /*点链表的数据结构*/
{
char ch;
struct node *next;
struct node *front;
};

int main()
{
struct node *head, *rear; /*head是头指针,指向点链表第一个节点*/
struct node *newnode; /*用于指向新申请的节点*/
struct node *oldnode; /*指向新节点的前一节点*/
char inputch;
int k=0;
while(1==1)
{
inputch=getch();
if(inputch==13)break;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->ch=inputch;

if(k==0)
{
head=newnode;
oldnode=newnode;

head->next = NULL;
head->front = NULL;
}
else
{
oldnode->next=newnode;
newnode->front = oldnode;
}

oldnode=newnode; /*使当前的新节点变成下一次的前一节点*/
k++;
}
oldnode->next = NULL;
rear = oldnode;

newnode=head;
while(newnode!=NULL)
{
printf("%c",newnode->ch);
newnode=newnode->next;
}
printf("\n");
newnode=rear;
while(newnode!=NULL)
{
printf("%c",newnode->ch);
newnode=newnode->front;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-25
加一个struct node *backnode;
首节点的backnode指向本身
让新创建的节点的backnode指向前一节点
相似回答