【程序72】
题目:创建一个链表。
1.程序分析:首先定义链表结构,给链表初始化,输出链表
2.程序源代码:
// example72.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int _tmain(int argc, _TCHAR* argv[]) { //my tao bao addbr
link ptr,head,tail; int num,i; tail = (link)malloc(sizeof(node)); tail->next = NULL; ptr = tail; printf("\nplease input the list data==>\n"); for(i = 0; i <= 4; i++) { printf("The %dth node data: ",(5-i)); scanf("%d",&num); ptr->data=num; head = (link)malloc(sizeof(node)); head->next = ptr; ptr = head; } ptr = ptr->next; i = 1; while(ptr!=NULL) { printf("The %dth node data is ==>%d\n", i, ptr->data); ptr = ptr->next; i++; } getchar(); getchar(); return 0; }
|
VS2005 compile and excute result:
please input the list data==>
The 5th node data: 5
The 4th node data: 4
The 3th node data: 3
The 2th node data: 2
The 1th node data: 1
The 1th node data is ==>1
The 2th node data is ==>2
The 3th node data is ==>3
The 4th node data is ==>4
The 5th node data is ==>5
阅读(823) | 评论(0) | 转发(0) |