zoukankan      html  css  js  c++  java
  • 建立简单的链表

    // test.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include <iomanip>
    using namespace std;
    
    //#define NULL 0
    struct student
    {long num;
    float score;
    student *next;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    student *head=NULL; //链表的头指针,初始化为NULL
        student *p=head; //p用来指向当前链表的尾结点
        student *s=NULL; //s用来指向新开辟的结点
        s=new student; //动态开辟第一个新结点,让指针s指向它
        /*为新结点赋值*/
        s->num=31001; 
        s->score=89.5;
        s->next=NULL;
        head=s; //头指针指向当前节点,作为链表中的第一个结点
        p=s;//p指向第一个结点,也是当前链表的尾结点(因为当前链表中只有一个结点)
        s=new student; //动态开辟第二个新结点,让指针s指向它
        /*为新结点赋值*/
        s->num=31003; 
        s->score=90;
        s->next=NULL;
        p->next=s; //将新结点连接到链表尾部
        p=p->next; //将p指向当前的尾结点
         s=new student; //动态开辟第二个新结点,让指针s指向它
        /*为新结点赋值*/
        s->num=31007; 
        s->score=85;
        s->next=NULL;
        p->next=s; //将新结点连接到链表尾部
        p=p->next; //将p指向当前的尾结点
        //遍历数组,从链表的一个结点开始访问直到链表尾结点
        p=head;
        do
        { 
          cout<<p->num<<" "<<p->score<<endl;//输出当前指针p所指的结点内容
          p=p->next; //当前指针p移动到下一个结点
        }while(p!=NULL);
        system("pause");
        return 0;
    }
  • 相关阅读:
    下拉框插件开发 (一)
    工厂模式 抽象工厂模式
    闭包 构建函数 简单工厂模式
    js继承
    浅谈js观察者模式
    浅谈js单例模式
    localstorage本地存储
    Vi 配置文件
    Vi Command
    我的Firs博客
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/5239070.html
Copyright © 2011-2022 走看看