动态链表的建立

建立一个动态链表就是在程序执行时根据用户的输入从无到有一次建立起一个表格,这个表格中的数据都一次保存在各个节点上,每个节点都是用new操作符来动态开辟,节点与节点之间用指针next相关联

代码示例

 1 #include <iostream>
 2 using namespace std;
 3 /*******************define class book************************/
 4 class book
 5 {
 6 public:
 7     int num;
 8     float price;
 9     book *next;            //the most important
10 };
11
12 book *head=NULL;
13 /*********************creat() function**********************/
14 book *creat()
15 {
16     book *p1,*p2;
17     p1=new book;
18     head=p1;
19     p2=p1;
20     /***************the first time********************/
21     cout<<"请输入图书的编号,以0结束"<<endl;
22     cin>>p1->num;
23     if(p1->num!=0)
24     {
25         cout<<"请输入图书的价格"<<endl;
26         cin>>p1->price;
27     }
28     else
29     {
30         delete p1;p2=NULL;p2->next=NULL;head=NULL;return head;
31     }
32     /**********************again and again********************/
33     while(p1->num!=0)
34     {
35         p2=p1;        //p2 point to an old node
36         p1=new book;//p1 point to a new node
37         cout<<"请输入图书的编号,以0结束"<<endl;
38     cin>>p1->num;
39     if(p1->num!=0)
40     {
41         cout<<"请输入图书的价格"<<endl;
42         cin>>p1->price;
43     }
44     p2->next=p1;//the old node‘s next point to a new node,so it begin circulating
45     }
46     /*************************finish*************************/
47     delete p1;//delete new node while finishing cin;
48     p2->next=NULL;
49     return head;
50 }
51 /*************************main() function*****************************/
52 int main()
53 {
54     creat();
55     return 0;
56 }

结果演示

随即开始循环不断地建立动态链表

时间: 2024-12-26 04:17:27

动态链表的建立的相关文章

C 动态链表的建立,输出,删除,插入

动态链表的建立,输出,删除,插入 #include<stdio.h> #include<malloc.h> #include<stdlib.h> #define NULL 0 #define LEN sizeof(struct student) struct student { long num; float score; struct student*next; }; int n;/*n为全局变量*/ struct student *creat() { struct

数据结构 单向动态链表的建立和输出

#include<stdio.h> #include<stdlib.h> struct student{ long int num;//学号 float score;//成绩 struct student*next;//指向下一个学生 }; int n=0;//有n个学生数据 /*创建链表函数*/ struct student* creat() { struct student *p1,*p2,*head; p2=p1=(struct student*)malloc(sizeof(

【c++程序】动态链表的建立

#include<iostream> using namespace std; class book { public: int num; float price; book *next; }; book *head=NULL; book *creat() { book *p1,*p2; p1=new book; head=p1; p2=p1; cout<<"请输入图书的编号,以0结束"<<endl; cin>>p1->num; i

c语言:写一个函数建立一个有3名学生数据的单向动态链表

写一个函数建立一个有3名学生数据的单向动态链表. 解:程序: #include<stdio.h> #include<stdlib.h> #define LEN sizeof(struct Student) struct Student { long num; float score; struct Student *next; }; int n; struct Student *creat(void)//定义函数返回一个指向链表头的指针 { struct Student *head

通过结构体,建立动态链表,并输出链表

/***************** * 通过结构体,建立动态链表,并输出链表. * *******************/ #include<stdio.h> #include<malloc.h> #include<stdlib.h> #define LEN sizeof(struct student)  //宏定义 将LEN 替换为student 结构体的大小数值 struct student *create();  //声明创建新链表(节点)(结构体)函数,返回

使用C语言描述静态链表和动态链表

静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链表而言的,一般地,在描述线性表的链式存储结构时如果没有特别说明即默认描述的是动态链表. 下面给出它们的简单实现,关于线性表更为详尽的C语言的实现,可以参考 http://www.cnblogs.com/choon/p/3876606.html 静态链表 #define _CRT_SECURE_NO_

链表的建立及释放

链表是建立 struct Data{    int num;    int score;    struct Data *next;}; struct Data *input() { struct Data *head,*p1,*p2; p1=(struct Data*)malloc(LEN); p2=(struct Data*)malloc(LEN); n=0; scanf("%d",&p1->num); while(p1->num!=0) { scanf(&qu

单向链表的建立(2)

链表的建立可以使用尾插法,也可以使用头插法,头插法就是从头节点开始,向前扩展节点,最后生成带头节点的单向链表,使得内容与输入相反. 链表的定义与(1)中相同,这里只介绍链表的建立函数. 1 LNode *create(int n) 2 { 3 int m; 4 LNode *head=(LNode *)malloc(sizeof(LNode)); 5 LNode *tail=(LNode *)malloc(sizeof(LNode)); 6 LNode *p; 7 head->next=NULL

数据结构之 线性表---有序链表的建立

mxjj130304杨少鹏(13110581086)        注销 数据结构实验之链表六:有序链表的建立 数据结构实验之链表六:有序链表的建立 Time Limit: 1000MS    Memory limit: 65536K 题目描述 输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表. 输入 第一行输入整数个数N: 第二行输入N个无序的整数. 输出 依次输出有序链表的结点值. 示例输入 6 33 6 22 9 44 5 示例输出 5 6 9 22 3