Chinaunix首页 | 论坛 | 博客
  • 博客访问: 453184
  • 博文数量: 142
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-03 21:00
文章分类

全部博文(142)

文章存档

2011年(3)

2010年(32)

2009年(107)

我的朋友

分类: C/C++

2009-10-29 10:31:54

【程序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) |
给主人留下些什么吧!~~