我有一個簡單的鏈接串列程式,它將創建/列印它,并在隨后列印它的最后兩個數字(按相反的順序)
cat link_list.c
/**。
* 創建和遍歷一個鏈接串列的C語言程式
*/
#include <stdio.h>
#include <stdlib.h>
/* 節點的結構 */
struct node {
int data; // Data
struct node *next; // Address
}*頭。
/*
*創建和顯示串列的函式
*/
void createList(int n)。
void traverseList()。
void ReverseList(struct node *)/span>。
int main()
{
int n。
printf("Enter the total number of nodes: ") 。
scanf("%d", &n);
createList(n)。
printf("
串列中的資料
")。)
traverseList()。
ReverseList(head)。
return 0;
}
/*
*創建一個n個節點的串列
*/
void createList(int n)
{
struct node *newNode, *temp; /span>
int data, i;
head = (struct node *)malloc(sizeof(struct node)) 。
//如果沒有分配到記憶體就終止。
if(head == NULL)
{
printf("Unable to allocate memory." )。
exit(0)。
}
//從用戶那里輸入節點的資料。
printf("輸入節點1的資料:")。
scanf("%d", &data)。
head->data = data; //鏈接資料域與資料。
head->next = NULL; //鏈接地址域為NULL。
///創建n - 1個節點并添加到串列中。
temp = head;
for(i=2; i<=n; i )
{
newNode = (struct node *)malloc(sizeof(struct node) )。)
/* If memory is not allocated for newNode */
if(newNode == NULL)
{
printf("Unable to allocate memory." )。
break。
}
printf("輸入節點%d的資料:", i)。
scanf("%d"/span>, &data)。
newNode->data = data; //鏈接newNode的資料域。
newNode->next = NULL; //確保新節點指向NULL。
temp->next = newNode; //將前一個節點與newNode相連。
temp = temp->next; //使當前節點成為前一個節點。
}
}
/*
*顯示整個串列
*/
void traverseList()
{
struct node *temp;
//span>如果串列為空,則回傳。
if(head == NULL)
{
printf("List is empty.") 。
return;
}
temp = head;
while(temp != NULL)
{
printf("Data = %d
", temp->data); //列印當前節點資料
temp = temp->next; //移到下一個節點。
}
}
static count=0, k=2;
ReverseList(struct node *head)
{
if (head == NULL)
return;
else {
ReverseList(head->next);
count 。
if (count <= k)
printf("Data = %d
", head->data)。)
}
}
對于一個輸入1 2 3,它將首先列印3 2 1,然后正確地列印3 2,但我對:
的問題感到困惑。 if (head == NULL)
return;
它用return;回傳的到底是什么,以及head后的指向
。 ReverseList(head->next);陳述句?
uj5u.com熱心網友回復:
假設我們有一個簡單的三節點串列,像這樣:
1 -> 2 -> 3
當我們最初呼叫ReverseList時,我們將1節點作為head,我們有這樣的呼叫鏈:
ReverseList(1 -> 2 -> 3 -> NULL)
ReverseList(2 -> 3 -> NULL)
ReverseList(3 -> NULL)
ReverseList(NULL)
printf(3)
printf(2)
printf(1)
uj5u.com熱心網友回復:
對于初學者來說,你忘了在函式定義中指定函式的回傳型別
ReverseList(struct node *head)
{
if (head == NULL)
return;
else {
ReverseList(head->next);
count 。
if (count <= k)
printf("Data = %d
", head->data)。)
}
}
你必須寫下
void ReverseList(struct node *head)
至于你的問題
我有一個困惑:
if (head == NULL)
return;
它用return;回傳的到底是什么,以及head在之后指向哪里 ReverseList(head->next);陳述句后指向哪里?
那么由于函式的回傳型別是void,并且回傳陳述句不包括運算式,那么回傳陳述句就不會回傳什么。
函式引數head是該函式的一個區域變數。該函式不會改變在檔案范圍內宣告的指標head
struct node {
int data; // Data
struct node *next; // Address
}*頭。
在函式的第一次呼叫中,由于這個陳述句,引數head的值是全域變數head的值
ReverseList(head);
如果值不等于NULL,那么函式就會遞回地呼叫自己,傳遞指標的值 head->next
ReverseList(head->next);
所以在函式本身的第二次遞回呼叫中,其引數head的值等于head->next的值,在這個運算式中head是函式前一次呼叫的函式引數。
也就是說,引數head首先得到全域變數head的值,然后引數被序列中下一個節點的資料成員next的值依次初始化。
因此,對于包含值為1、2、3的節點的串列,你有
。第1次呼叫 引數 head 等于全域變數 head
第 2 次呼叫
第2次呼叫
第3次呼叫 引數head等于head->next->next,指向數值為2的節點
第3次呼叫引數head等于head->next->next,指向數值為2的節點
第4次呼叫引數head等于head->next->next。
第4次呼叫 引數head等于head->next->next->next,指向數值為3的節點
第5次呼叫 引數head等于head->next->next->next->next并等于NULL請注意,在函式中使用靜態全域變數不是一個好主意
static count=0, k=2。
如果你想再呼叫一次函式,你需要記得重新初始化它們。最好是將變數k作為一個函式引數。
這個函式可以按照下面的演示程式中的方式來定義。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next; /span>
};
int push( struct節點 **head, int data )
{
struct node *new_node = malloc( sizeof( *new_node ) ) 。
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
return success;
}
void display( const struct node *head )
{
for ( ; head != NULL; head = head-> next )
{
printf( "%d -> ", head->data ) 。
}
puts( "null" ) 。
}
size_t display_last_n( const struct節點 *head, size_t n )。
{
size_t i = 0;
if ( head != NULL )
{
i = display_last_n( head-> next, n ) 1;
if ( !( n < i ))
{
if ( i != 1 ) printf( ", " ) 。
printf( "%d", head-> data );
}
}
return i;
}
void clear( struct node **head )
{
while ( *head )
{
struct node *tmp = *head;
*head = ( *head )->接下來。
free( tmp )。
}
}
int main(void)
{
struct node *head = NULL。
const int N = 10;
for ( int n = N; n-; )
{
push( &head, n ) 。
}
display( head );
for ( size_t i = 0; i < N; i )
{
display_last_n( head, i 1 ) 。
putchar( '
' )。
}
clear( &head )。
return 0;
}
程式輸出是
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> null
9, 8
9, 8, 7
9, 8, 7, 6.
9, 8, 7, 6, 5.
9, 8, 7, 6, 5, 4.
9, 8, 7, 6, 5, 4, 3.
9, 8, 7, 6, 5, 4, 3, 2.
9, 8, 7, 6, 5, 4, 3, 2, 1。
9, 8, 7, 6, 5。4, 3, 2, 1, 0。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322485.html
標籤:
上一篇:查找C語言絕對差異的程式中的錯誤
下一篇:自我參考的結構宣告
