复习三:链表——数据结构的前奏之建立链表

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖无为

感谢你们的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值