【发布时间】:2020-12-11 06:56:02
【问题描述】:
我的双向链表有两个虚拟节点,头和尾。 addToFirst 函数非常适合我,但是当我尝试使用与 addToBack 相同的算法时,它会一直为我打印虚拟节点。为什么会这样?
DList *init( )
{
head = ( DList * ) malloc( sizeof( DList ) );
if ( head == NULL ) {
prtError( "Insufficient memory!" );
return( NULL );
}
tail = ( DList * ) malloc( sizeof( DList ) );
if ( tail == NULL ) {
prtError( "Insufficient memory!" );
return( NULL );
}
head->data = -1;
tail->data = -1;
head->next = tail;
head->prev = NULL;
tail->next = NULL;
tail->prev = head;
return ( head ); /* indicates no error */
}
/* print function */
void prtList( )
{
DList *p;
for ( p = head->next; p != tail; p = p->next )
printf( "%4d", p->data );
printf( "\n");
}
DList *insertFirst( int d )
{
/***** ADD YOUR CODE HERE *****/
DList *first = ( DList * ) malloc( sizeof( DList ) );
printf("FF");
if ( first == NULL ) {
prtError( "Insufficient memory!" );
return( NULL );
}
/* 2. put in the data */
first->data = d;
/* 3. Make next of new node as head and previous as NULL */
first->next = (head)->next;
first->prev = NULL;
/* 4. change prev of head node to new node */
if ((head) != NULL)
(head)->prev = first;
/* 5. move the head to point to the new node */
(head)->next = first;
return first; // replace this line with your code
}
DList *insertLast( int d )
{
DList *last = ( DList * ) malloc( sizeof( DList ) );
printf("LL");
if ( last == NULL ) {
prtError( "Insufficient memory!" );
return( NULL );
}
/* 2. put in the data */
last->data = d;
/* 3. Make next of new node as head and previous as NULL */
last->prev = tail->prev;
last->next = NULL;
/* 4. change prev of head node to new node */
if ((tail) != NULL)
(tail)->next = last;
/* 5. move the head to point to the new node */
(tail) = last;
//if I try with (tail)->prev = last; then the outputs were empty.
return last; // replace this line with your code
}
预期结果:
LL 5
LL 5 10
LL 5 10 15
LL 5 10 15 20
LL 5 10 15 20 25
LL 5 10 15 20 25 30
LL 5 10 15 20 25 30 35
LL 5 10 15 20 25 30 35 40
LL 5 10 15 20 25 30 35 40 45
LL 5 10 15 20 25 30 35 40 45 50
FF 5 5 10 15 20 25 30 35 40 45 50
FF 10 5 5 10 15 20 25 30 35 40 45 50
FF 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 30 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 35 30 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 40 35 30 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 45 40 35 30 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
FF 50 45 40 35 30 25 20 15 10 5 5 10 15 20 25 30 35 40 45 50
我的结果:
LL -1
LL -1 5
LL -1 5 10
LL -1 5 10 15
LL -1 5 10 15 20
LL -1 5 10 15 20 25
LL -1 5 10 15 20 25 30
LL -1 5 10 15 20 25 30 35
LL -1 5 10 15 20 25 30 35 40
LL -1 5 10 15 20 25 30 35 40 45
FF 5 -1 5 10 15 20 25 30 35 40 45
FF 10 5 -1 5 10 15 20 25 30 35 40 45
FF 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 30 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 35 30 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 40 35 30 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 45 40 35 30 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
FF 50 45 40 35 30 25 20 15 10 5 -1 5 10 15 20 25 30 35 40 45
【问题讨论】:
-
请显示minimal reproducible example 输入和预期输出
-
不是主要问题,但我发现
void prtList( )存在一些问题,您使用的是p = head->next;和p->data,那么head->data呢 -
我不能更改打印功能。 :C
标签: c linked-list doubly-linked-list