2.鍊錶的一些練習
(1)輸出尾結點元素
#include
#include
#define n 5
typedef struct node
elemsn;
elemsn * createlink1(int a);
void printlink(elemsn *h);
int main()
;elemsn *head = null;
head = createlink1(a);
printlink(head);
printf("\n");
return 0;
}elemsn * createlink1(int a)
return h;
}void printlink(elemsn *h)
else}}
(2)輸出節點的個數
#include
#include
#define n 5
typedef struct node
elemsn;
elemsn * createlink1(int a);
int printlink(elemsn *h);
int main()
;elemsn *head = null;
head = createlink1(a);
t = printlink(head);
printf("\n");
return 0;
}elemsn * createlink1(int a)
return h;
}int printlink(elemsn *h)
else
}return i;
}(3)輸出節點中資料域為奇數的節點數總共有多少個
#include
#include
#define n 5
typedef struct node
elemsn;
elemsn * createlink1(int a);
int printlink(elemsn *h);
int main()
;elemsn *head = null;
head = createlink1(a);
t = printlink(head);
printf("奇數的有%d個",t);
printf("\n");
return 0;
}elemsn * createlink1(int a)
return h;
}int printlink(elemsn *h)
return cnt;
}(4)輸出單向鍊錶最大值
#include
#include
#define n 5
typedef struct node
elemsn;
elemsn * createlink1(int a);
elemsn * pmaxnode(elemsn *h);
int main()
;elemsn *head = null;
elemsn *p;
head = createlink1(a);
p = pmaxnode(head);
printf("max = %d",p->data);
printf("\n");
return 0;
}elemsn * createlink1(int a)
return h;
}elemsn * pmaxnode(elemsn *h)
}return pmax;
}(5)逆向輸出單向鍊錶
#include
#include
#define n 5
typedef struct node
elemsn;
elemsn * createlink1(int a);
void * printlink(elemsn *h);
int main()
;elemsn *head = null;
head = createlink1(a);
printlink(head);
printf("\n");
return 0;
}elemsn * createlink1(int a)
return h;
}void * printlink(elemsn *h)
return pend;
}終極版:合併為乙個函式
#include
#include
#define n 5
typedef struct node
elemsn;
//以下對應函式在主函式後按順序排列
elemsn *createlink1(int a);
elemsn *ptailnode(elemsn *h);
int countnode(elemsn *h);
int countodd(elemsn *h);
elemsn *maxnode(elemsn *h);
void printlink(elemsn *h);
int main()
;elemsn *head = null;
elemsn *ptail,*pmax;
int count,countodd;
//建立單向鍊錶
head = createlink1(a);
//輸出尾結點的值
ptail = ptailnode(head);
printf("尾結點的值是:%d\n",ptail->data);
//輸出總結點數
count = countnode(head);
printf("總結點個數為:%d\n",count);
//輸出節點資料域中儲存的是奇數的節點個數
countodd = countodd(head);
printf("存的數是奇數的節點數共有%d個\n",countodd);
//輸出單向鍊錶中的最大值
pmax = maxnode(head);
printf("最大節點max = %d\n",pmax->data);
//逆向輸出單向鍊錶
printf("逆向順序輸出結果為:");
printlink(head);
printf("\n");
return 0;
}//建立鍊錶
elemsn *createlink1(int a)
else
}return h;
}//求尾結點的值
elemsn *ptailnode(elemsn *h)
//求節點的個數
int countnode(elemsn *h)
//求資料域中值為奇數的節點個數
int countodd(elemsn *h)
return cnt;
}//求節點中的最大值
elemsn *maxnode(elemsn *h)
}return pmax;
}//逆向輸出鍊錶
void printlink(elemsn *h)
}
C語言資料結構之鍊錶部分(10)
10.練習 統計單向鍊錶的每個元素出現的次數 include include define n 11 typedef struct node elemsn elemsn createlink int a void printlink elemsn h int main 建立 head createl...
C語言資料結構之鍊錶部分(11)
11.建立單向迴圈鍊錶 include include typedef struct node elemsn elemsn createlink int a,int n void printlink elemsn h elemsn delkeynode elemsn h,int key int ma...
資料結構 2 鍊錶 C語言練習
用p從頭到尾掃瞄單鏈表,pre指向 p節點的前驅。若p的節點是x則刪除,並讓p後移,否則,pre和p同時移動 void del x 1 linklist l,elemtype x else 尾插法 值不為x則接入l,否則釋放 void del x 2 linklist l,elemtype x el...