Writted by Bruth_Lee in Southwest universiy of Science and Technology.
//建立链表:一
//最普通的方法#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
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;
}