C語言實現的鏈表結構
最近沒事學學C語言的編程,通過指針編寫了一個鏈表結構,代碼比較簡單,不做介紹,詳細代碼如下:
/* * File: main.c * Author: wuguowei * * Created on 2015年2月12日, 上午11:54 */ #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct Student) struct Student { long num; float score; struct Student *next; }; int n; struct Student * create(void) { struct Student *head; struct Student *p1, *p2; n = 0; p1 = p2 = (struct Student *) malloc(LEN); scanf("%ld,%f", &p1->num, &p1->score); head = NULL; while (p1->num != 0) { n = n + 1; if (n == 1) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct Student *) malloc(LEN); scanf("%ld,%f", &p1->num, &p1->score); } p2->next = NULL; return head; } void print(struct Student *head) { struct Student *p; printf("\nNow,These %d records are:\n", n); p = head; if (head != NULL) { do { printf("%ld %5.1f\n", p->num, p->score); p = p->next; } while (p != NULL); } } /* * */ int main(int argc, char** argv) { struct Student * pt; pt = create(); print(pt); return (EXIT_SUCCESS); }
執行結果:
輸入: 1001,89.3 1002,99 1003,79.4 1004,99.8 0,0 Now,These 4 records are: 1001 89.3 1002 99.0 1003 79.4 1004 99.8 運行 FINISHED; 退出值0; 實時: 37s; 用戶: 0ms; 系統: 0ms
本文由用戶 aiguang 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!