日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单链表的建立和打印

發布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单链表的建立和打印 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h> #include <stdlib.h>//定義鏈表數據結構 struct node {int num;struct node *next; };//函數聲明 struct node *creat(struct node *); void print(struct node *);void main() {struct node *head;head = NULL; //創建一個空表head = creat(head); //創建單鏈表print(head); //打印單鏈表 }struct node *creat(struct node *head) {struct node *p1,*p2;int i = 1;//利用malloc函數向系統申請節點p1 = p2 = (struct node *)malloc(sizeof(struct node)); //申請節點printf("輸入大于0的值,小于等于0則結束,值的存放地址為:p1_addr = %d\n",p1);scanf("%d",&p1->num);p1->next = NULL;while(p1->num > 0){if(head == NULL)head = p1;elsep2->next = p1;p2 = p1;p1 = (struct node *)malloc(sizeof(struct node));p1->next = NULL;//置空i++;printf("輸入大于0的值,小于等于0則結束,值的存放地址為:p%d_addr = %d\n",i,p1);scanf("%d",&p1->num);}free(p1); p1 = NULL;p2->next = NULL;printf("輸入結束\n");return head; }void print(struct node *head) {struct node *tmp;tmp = head;printf("鏈表打印開始!!!\n");while(tmp != NULL){printf("輸入的值為:num = %d,地址為:addr = %d\n",tmp->num,tmp);tmp = tmp->next;}printf("鏈表打印結束!!!\n"); }

運行結果:?

總結

以上是生活随笔為你收集整理的单链表的建立和打印的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。