复习三:链表——数据结构的前奏之建立链表
2018-01-09 20:37
316 查看
Writted by Bruth_Lee in Southwest universiy of Science and Technology.
//建立链表:一
//最普通的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
4000
{
int value;
struct node *next;
}Node;
int main()
{
int number;
Node *p=NULL;
Node *head=NULL;
//Creat a linked_list --建立链表
do//Enter the element of the list,if(element==-1),then break;--输入链表的元素,输入-1控制结束
{
cin>>number;
if(number!=-1)
{
p=(Node *)malloc(sizeof(Node));//Open up memery开辟一块空间
p->value=number;//assignment赋值
p->next=NULL;//When I open up memery,the next memery is NULL;--当开辟一块内存的时候,它的下一个应该是空
Node * last=head;//Set up a tail pointer--建立一个尾指针
if(last)
{
while(last->next) last=last->next;//The aim is to move the tail pointer to the end--目的是将尾指针移到末尾
last->next=p;//add to the linked_list--加到链表末尾
}
else head=p;//First,the head pointer is NULL,adding to the linked_list directly;--第一次头指针为空<,直接加到链表的末尾
}
}while(number!=-1);
return 0;
}
//建立链表:二
//稍稍欠缺功夫的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node * next;
}Node;
it main()
{
Node * add(Node *head,number);
int number;
Node * head=NULL;
while(cin>>number && number!=-1)
{
head=add(head,number);
}
return 0;
}
Node * add(Node *head,number)
{
Node *p=(Node *)malloc(sizeof(Node));
p-value=number;
p->next=NULL;
Node *last=head;
if(last)
{
while(last->next) last=last->next;
last->next=p;
}
else head=p;
return head;
}
//建立链表:三
//比较实用的方法
//We should set up a function to creat a list,it's more concise--我们应当建立一个函数来建立链表,显得更加简洁
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node * next;
}Node;
int main()
{
void add(Node**head,int number);
Node *head;
int number;
while(cin>>number && number!=-1)
{
add(&head,number);//You need use a pointer to a pointer to modify the pointer,If you don't understand this ,please click http://blog.csdn.net/qq_40883132/article/details/78984817--需要用指向指针的指针来对指针进行修改,如果你不明白这点,请点击http://blog.csdn.net/qq_40883132/article/details/78984817 }
return 0;
}
void add(Node**head,int number)//You need use a pointer to a pointer to modify the pointer,and the reference can be used,the corresponding modification as follow;--用指向指针的指针来修改指针,也可以用引用,对应的修改为:add(head,number); void add(Node*&head,int number);
{
Node *p=(Node *)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last=head;
if(last)
{
while(last->next) last=last->next;
last->next=p;
}
else head=p;
}
//建立链表:四
//最常用的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node *next;
}Node;
typedef struct_list{
Node *head;
Node *tail;
}List;
int main()
{
void add(List *list,int number);
int number;
List list;//Set up a chain called list--建立一个叫list的链表
list.head=list.tail=NULL;
while(cin>>number && number!=-1)
{
add(&list,number);
}
return 0;
}
void add(List *list, int number)
{
Node *p = (Node *)malloc(sizeof(Node));
p->value = number;
p->next= NULL;
if (list->head) list->tail->next = p;
else
list->head = p;
list->tail = p;
}
//建立链表:一
//最普通的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
4000
{
int value;
struct node *next;
}Node;
int main()
{
int number;
Node *p=NULL;
Node *head=NULL;
//Creat a linked_list --建立链表
do//Enter the element of the list,if(element==-1),then break;--输入链表的元素,输入-1控制结束
{
cin>>number;
if(number!=-1)
{
p=(Node *)malloc(sizeof(Node));//Open up memery开辟一块空间
p->value=number;//assignment赋值
p->next=NULL;//When I open up memery,the next memery is NULL;--当开辟一块内存的时候,它的下一个应该是空
Node * last=head;//Set up a tail pointer--建立一个尾指针
if(last)
{
while(last->next) last=last->next;//The aim is to move the tail pointer to the end--目的是将尾指针移到末尾
last->next=p;//add to the linked_list--加到链表末尾
}
else head=p;//First,the head pointer is NULL,adding to the linked_list directly;--第一次头指针为空<,直接加到链表的末尾
}
}while(number!=-1);
return 0;
}
//建立链表:二
//稍稍欠缺功夫的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node * next;
}Node;
it main()
{
Node * add(Node *head,number);
int number;
Node * head=NULL;
while(cin>>number && number!=-1)
{
head=add(head,number);
}
return 0;
}
Node * add(Node *head,number)
{
Node *p=(Node *)malloc(sizeof(Node));
p-value=number;
p->next=NULL;
Node *last=head;
if(last)
{
while(last->next) last=last->next;
last->next=p;
}
else head=p;
return head;
}
//建立链表:三
//比较实用的方法
//We should set up a function to creat a list,it's more concise--我们应当建立一个函数来建立链表,显得更加简洁
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node * next;
}Node;
int main()
{
void add(Node**head,int number);
Node *head;
int number;
while(cin>>number && number!=-1)
{
add(&head,number);//You need use a pointer to a pointer to modify the pointer,If you don't understand this ,please click http://blog.csdn.net/qq_40883132/article/details/78984817--需要用指向指针的指针来对指针进行修改,如果你不明白这点,请点击http://blog.csdn.net/qq_40883132/article/details/78984817 }
return 0;
}
void add(Node**head,int number)//You need use a pointer to a pointer to modify the pointer,and the reference can be used,the corresponding modification as follow;--用指向指针的指针来修改指针,也可以用引用,对应的修改为:add(head,number); void add(Node*&head,int number);
{
Node *p=(Node *)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last=head;
if(last)
{
while(last->next) last=last->next;
last->next=p;
}
else head=p;
}
//建立链表:四
//最常用的方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int value;
struct node *next;
}Node;
typedef struct_list{
Node *head;
Node *tail;
}List;
int main()
{
void add(List *list,int number);
int number;
List list;//Set up a chain called list--建立一个叫list的链表
list.head=list.tail=NULL;
while(cin>>number && number!=-1)
{
add(&list,number);
}
return 0;
}
void add(List *list, int number)
{
Node *p = (Node *)malloc(sizeof(Node));
p->value = number;
p->next= NULL;
if (list->head) list->tail->next = p;
else
list->head = p;
list->tail = p;
}
相关文章推荐
- 复习四:链表——数据结构的的前奏之链表元素的遍历、删除以及链表中元素的删除
- 单向链表的建立,插入,删除(复习一下)
- 数据结构一一线性表的链式存储结构之头插法和尾插法建立链表
- 复习数据结构链表插入排序
- 数据结构学习笔记7--c语言建立一个链表(已测正确无误)
- 数据结构 线性表 建立循环链表
- 数据结构---单链表(建立,节点删除,节点插入)
- 第4周SHH数据结构—【项目1-建立单链表】
- 算法数据结构复习[单链表]
- C语言数据结构 双向链表的建立与基本操作
- 【郝斌数据结构自学笔记】57-59_递归8 _ 汉诺塔_1线性结构总复习 2线性结构和非线性结构关系 3栈队列链表数组之间的关系【重点】
- 数据结构复习-链表练习程序
- 2117 数据结构实验之链表二:逆序建立链表
- 数据结构——链表之顺序建立链表
- 数据结构系列之链表——单链表的建立
- 【数据结构复习】线性表的链式存储--单链表
- 复习基础-数据结构-链表介绍( 1 )
- 数据结构,空链表的建立
- 【数据结构复习】链表相关
- 数据结构复习——线性表(二)链表