已知head指向帶頭結點的單詞連結串列,連結串列中每個結點包含資料long和指向被解構結點的指標

2021-09-05 12:01:08 字數 3603 閱讀 2688

1樓:

node* reverse(node * head) //如果不帶返回值,引數要寫成node ** head ,涉及到引數值傳遞和地址傳遞問題

else

while(head != null) //思想是從待翻轉的連結串列中依次取一個節點,每次取一個都放在臨時儲存的連結串列的最前面

head = tmp;

return head;

}以上是最好的方法,還有一種就是先遍歷連結串列,把每個節點的指標儲存在一個陣列中,然後從陣列最後開始,反過來重新構建連結串列,這樣空間複雜度高,但是簡單

2樓:碧海風雲

#include

#include

#include

#define elemtype long /*元素型別*/

#define elemprinttype "%ld\t" /*元素列印型別*/

#define status int

#define overflow -1

#define error 0

#define ok 1

/* 單連結串列資料結構 */

typedef struct lnode  lnode, *linklist;

/******************************** 以下為函式宣告 ********************************/

void initlist (linklist *l); /* 初始化 */

void destroylist (linklist l); /* 銷燬 */

status listisempty (linklist l); /* 判斷單連結串列是否為空 */

int listlength (linklist l); /* 獲取單連結串列長度 */

status listinsertnode (linklist l, int i, elemtype e); /* 單連結串列指定位置插入新元素 */

status listprint (linklist l); /* 輸出連結串列 */

status listreverse (linklist l); /* 逆置連結串列 */

/******************************** 以上為函式宣告 ********************************/

/* 初始化 */

/* 操作結果:構造一個空的單連結串列l */

void initlist (linklist *l)

/* 銷燬 */

/* 初始條件:單連結串列l已存在。操作結果:銷燬單連結串列l */

void destroylist (linklist l)

free (l);

}/* 判斷單連結串列是否為空 */

/* 初始條件:單連結串列l已存在。操作結果:若l為空表,則返回true,否則返回false */

status listisempty (linklist l)

/* 獲取單連結串列長度 */

/* 初始條件:單連結串列l已存在。操作結果:返回l中資料元素個數 */

int listlength (linklist l)

return i;

}/* 單連結串列指定位置插入新元素 */

/* 操作結果:在帶頭結點的單連結串列l中第i個位置之前插入元素e */

status listinsertnode (linklist l, int i, elemtype e)

if (!p || j>i-1) /* 插入位置不合理:i小於1或者大於表長 */

return error;

/* 生成新結點,並插入l中 */

s = (linklist) malloc (sizeof (struct lnode));

s->data = e;

s->next = p->next;

p->next = s;

return ok;

}/* 輸出連結串列 */

status listprint (linklist l)

linklist p = l->next; /* p指向第一個結點 */

while (p!=null)

putchar ('\n');

return ok;

}/* 逆置連結串列 */

/* 初始條件:單連結串列l已存在。操作結果:連結串列元素逆置 */

status listreverse (linklist l)

return ok;

}int main (void)

執行結果

c語言,編寫程式。已知head指向一個帶頭結點的單向連結串列,連結串列中每個結點包含資料域(date)和指

3樓:匿名使用者

#include

#include

typedef struct _node

node,*list;

int getmax_value(list head)node *ptr = head->next;

int max = ptr->data;

while (ptr->next != null)}return max;

}node* getmax_address(list head)node *ptr = head->next;

node *max_address = ptr;

while (ptr->next != null)}return max_address;

}list creatlist(int num)//num為建立節點個數

return head;

}void freelist(list head)}int main()

已知head指向一個帶頭結點的單向連結串列,連結串列中每個結點包含字元型資料域(data)和指標域(next)。請編寫函 5

4樓:千里

已知head指向一個帶頭結點的單向連結串列連結串列中每個結點包含字元型資料域data和指標域next。請編寫函式實現連結串列的逆置。

5樓:光之琉璃影之殤

#include

#include

typedef struct node

node,*pnode;

pnode create(void);

void output(pnode head);

void inverse(pnode head);

int main()

pnode create(void)

;//使用陣列初始化連結串列,遇0連結串列結束,0不存入連結串列pnode head=(pnode)malloc(sizeof(node));

pnode p,q;

head->next=null;

p=head;

for(i=0;a[i]!=0;i++)

return head;

}void output(pnode head)printf("\n");

}void inverse(pnode head)p->next=head->next;

head->next=p;}

在具有n個結點的有序單連結串列中插入新結點並仍然保持有序的時間複雜度是為什麼是O(n

因為單連結串列儲存bai的資訊只du有表頭 如果zhi要在特定位置插入dao一個節點 需要先從表頭內一路找到那個節容點。數量級遞增排列,常見的時間複雜度有 常數階o 1 對數階o 線性階o n 線性對數階o nlog2n 平方階o n 2 立方階o n 3 k次方階o n k 指數階o 2 n 隨著...

已知函式fxxaxa,aR討論函式fx的單調性

f x x3 ax a,a bair,f x 3x2 a,du a 0時,f x 0恆成立,所以zhif x 單調遞增。a 0時,f x 0,則daox 專 a 3 或x a 3 時,f x 0,f x 單調增區間是屬x a 3 和x a 3 f x 0,則 a 3 f x 單調減區間是x a 3 ...

已知一棵度為m的樹中有 n度為1的結點,n度為2的結點nm個度為m的結點

設總共有n個節點 顯然就有 n n0 n1 n2 nm 其中no就表示葉子節點而除了根節點外每個節點都由別的結點引出 n 1 0 n0 1 n1 2 n2 m nm聯立兩個等式得 n0 1 n2 2n3 m 1 nm非終端節點就是非葉子節點了也就是 n1 n2 n3 nm 在樹中除根外,每個結點有且...