我们今天来温习单向链表的种种操作。
1.普通的链表创建
对于创建链表有一个很重要的思想。那就是每次一定只创建一个节点。这样创建链表的难度就被大大降低了。
我们的算法思维是这样的。所谓创建,不过是把新的节点插入到链表尾部。所以我们做的事情很简单,只需要两步:1.找到尾节点2.插入新节点
//我们来做链表
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int value;
struct node *next;
}Node;
int len=sizeof(Node);
Node* creat_normal(Node *head,int value);//一定每次创建一个节点在最末尾位置
int main()
{
int n;
Node *head=(Node *)malloc(len);//使用头节点
head->next=NULL;//千万不敢忘记!!!
Node *p=NULL;
do{
scanf("%d",&n);
if(n==0) break;
head=creat_normal(head,n);
}while(1);//为了讲解就这么简单了
for(p=head->next;p;p=p->next){
printf("%d\n",p->value);
}
return 0;
}
Node* creat_normal(Node *head,int value)//一定每次创建一个节点在最末尾位置
{
Node *pnew,*tail=head;
pnew=(Node *)malloc(len);
pnew->value=value;
pnew->next=NULL;
while(tail->next!=NULL) tail=t