- 创建 输出 查找
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int value;
struct _node *next;
}Node; //相当于结构体类型名,和int类型类似
typedef struct _List
{
Node *head;
}List; //与Node 类似。
void add(List* plist,int number);
void print(List *plist);
int main(int argc,char const *argv[])
{
int number;
//Node * head=NULL; //头指针表示的是哪一个链表
List list;
list.head=NULL;
do
{
scanf("%d",&number);
if(number != -1)
{
add(&list,number);
}
}while(number != -1);
print(&list);
int isFound=0;
scanf("%d",&number);
Node *p; //定义一个Node类型的指针变量p //声明一个结构类型可以用来定义很多个这样的变量,每一个这种类型的变量都会像声明中的那样有各种变量。
for(p=list.head;p;p=p->next) //本地变量如果没有赋初始值则存的是一些乱七八糟的值,而全局变量为负值默认为0。
{ //在结构体中,若某个成员变量未赋值,则默认为0。
if(p->value == number) //结构类型是没有实体的,定义一种结构类型,只是告诉编译器所有这种结构类型的变量长得像这个样子。
{ //结构变量是实体。在点的右边。
printf("找到了!\n");
isFound=1;
break;
}
}
if(!isFound)
{
printf("没找到\n");
}
Node *q;
for(q=NULL,p=list.head;p;q=p,p=p->next)
{
if(p->value == number)
{
if(p==NULL)
{
list.head=p->next;
free(p);
}
else
{
q->next =p->next ;
free(p);
break;
}
}
}
print(&list);
return 0;
}
void add(List* plist,int number)
{
//add to linked-list
Node * p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last = plist->head;//找到最后一个,接上去
if(last)//确保链表不是空的
{
while(last->next)//last不是NULL的话
{
last=last->next;//last不断向后遍历链表,直到找到链表原来的最后一个节点,即指向NULL的那个节点。
}
last->next=p;
}
else
plist->head = p;
}
void print(List *plist)
{
Node *p;
for(p=plist->head;p;p=p->next)
printf("%d\t",p->value);
printf("\n");
};
- 1+ 删除
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int value;
struct _node *next;
}Node;
typedef struct _List
{
Node *head;
}List;
void add(List* plist,int number);
void print(List *plist);
int main(int argc,char const *argv[])
{
int number;
//Node * head=NULL; //头指针表示的是哪一个链表
List list;
list.head=NULL;
do
{
scanf("%d",&number);
if(number != -1)
{
add(&list,number);
}
}while(number != -1);
print(&list);
int isFound=0;
scanf("%d",&number);
Node *p;
for(p=list.head;p;p=p->next)
{
if(p->value == number)
{
printf("找到了!\n");
isFound=1;
break;
}
}
if(!isFound)
{
printf("没找到\n");
}
Node *q;
for(q=NULL,p=list.head;p;q=p,p=p->next)
{
if(p->value == number)
{
if(p==NULL)
{
list.head=p->next;
free(p);
}
else
{
q->next = p->next ;
free(p);
break;
}
}
}
print(&list);
return 0;
}
void add(List* plist,int number)
{
//add to linked-list
Node * p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last = plist->head;//找到最后一个,接上去
if(last)//确保链表不是空的
{
while(last->next)//last不是NULL的话
{
last=last->next;//last不断向后遍历链表,直到找到链表原来的最后一个节点,即指向NULL的那个节点。
}
last->next=p;
}
else
plist->head = p;
}
void print(List *plist)
{
Node *p;
for(p=plist->head;p;p=p->next)
printf("%d\t",p->value);
printf("\n");
};
3.清除
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int value;
struct _node *next;
}Node;
typedef struct _List
{
Node *head;
}List;
void add(List* plist,int number);
void print(List *plist);
int main(int argc,char const *argv[])
{
int number;
//Node * head=NULL; //头指针表示的是哪一个链表
List list;
list.head=NULL;
do
{
scanf("%d",&number);
if(number != -1)
{
add(&list,number);
}
}while(number != -1);
print(&list);
int isFound=0;
scanf("%d",&number);
Node *p;
for(p=list.head;p;p=p->next)
{
if(p->value == number)
{
printf("找到了!\n");
isFound=1;
break;
}
}
if(!isFound)
{
printf("没找到\n");
}
Node *q;
for(q=NULL,p=list.head;p;q=p,p=p->next)
{
if(p->value == number)
{
if(p==NULL)
{
list.head=p->next;
free(p);
}
else
{
q->next = p->next ;
free(p);
break;
}
}
}
print(&list);
return 0;
}
void add(List* plist,int number)
{
//add to linked-list
Node * p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last = plist->head;//找到最后一个,接上去
if(last)//确保链表不是空的
{
while(last->next)//last不是NULL的话
{
last=last->next;//last不断向后遍历链表,直到找到链表原来的最后一个节点,即指向NULL的那个节点。
}
last->next=p;
}
else
plist->head = p;
}
void print(List *plist)
{
Node *p;
for(p=plist->head;p;p=p->next)
printf("%d\t",p->value);
printf("\n");
};
- 头插法
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int value;
struct _node *next;
}Node;
Node * createLink()
{
Node *head = (Node*)malloc(sizeof(Node));
head->next = NULL;
int number;
do
{
scanf("%d",&number);
if(number != -1)
{
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
if(head->next)
{
p->next = head->next;
head->next = p;
}
else
{
head->next =p;
p->next = NULL;
}
}
}while(number != -1);
return head;
}
void print(Node *head)
{
Node *p=head->next;
for(;p;p=p->next)
{
printf("%d ",p->value);
}
}
int main()
{
Node* head=createLink();
print(head);
}
- 尾插法
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node *next;
}Node;
Node *add();
void print(Node *head);
int main()
{
Node *head = add();
print(head);
}
Node *add()
{
Node *head = NULL;
int number;
scanf("%d",&number);
while(number != -1)
{
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;
} //最后while循环结束后,last指向新节点加入之前的最后的节点
else
{
head = p; //此时链表为空,直接将
}
scanf("%d",&number);
}
return head;
}
void print(Node *head)
{
Node *p = head ;
Node *q = NULL;
for(;p;p=p->next )
{
printf("%2d",p->value );
}
}