创建链表:
头插法:
struct Link* init_head_insert(struct Link* head)
{
struct Link* new;
struct Link* p=head;
new=(struct Link*)malloc(sizeof(struct Link));
puts("input node");
scanf("%d",&new->date);
new->next=NULL;
if(p!=NULL){
new->next=p;
p=new;
}else{
p=new;
}
return p;
}
尾插法:
struct Link* init_tail_insert(struct Link* head)
{
struct Link* new;
struct Link* p=head;
new=(struct Link*)malloc(sizeof(struct Link));
puts("input node");
scanf("%d",&new->date);
new->next=NULL;
if(head==NULL){
head=new;
return head;
}
while(p!=NULL){
if(p->next==NULL){
p->next=new;
return head;
}
p=p->next;
}
}
遍历链表:
void printLink(struct Link* head)
{
struct Link* p=head;
while(p!=NULL){
printf("%d ",p->date);
p=p->next;
}
printf("\n");
}
增加新节点:
在指定节点前加入新节点:
struct Link* add_head_insert(struct Link* head)
{
int N;
struct Link* new=(struct Link*)malloc(sizeof(struct Link));
struct Link* p=head;
puts("where is insert new node");
scanf("%d",&N);
puts("input new node");
scanf("%d",&new->date);
new->next=NULL;
if(p->date==N){
new->next=p;
p=new;
return p;
}
while(p->next!=NULL){
if(p->next->date==N){
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
}
在指定节点后加入新节点:
void add_tail_insert(struct Link* head)
{
int N;
struct Link* new=(struct Link*)malloc(sizeof(struct Link));
struct Link* p=head;
puts("where is insert new node");
scanf("%d",&N);
puts("input new node");
scanf("%d",&new->date);
new->next=NULL;
while(p!=NULL){
if(p->date==N){
new->next=p->next;
p->next=new;
break;
}
p=p->next;
}
}
删除节点:
第一种情况:
第二种情况:
struct Link* delete_Link(struct Link* head)
{
int N;
struct Link* p=head;
puts("input delete node");
scanf("%d",&N);
if(p->date==N){
p=p->next;
return p;
}
while(p->next!=NULL){
if(p->next->date==N){
p->next=p->next->next;
return head;
}
p=p->next;
}
}
修改节点:
void modify_Link(struct Link* head)
{
int N;
int new;
struct Link* p=head;
puts("modify that node");
scanf("%d",&N);
puts("enter the modified link ");
scanf("%d",&new);
while(p!=NULL){
if(p->date==N){
p->date=new;
break;
}
p=p->next;
}
}
查找节点:
void find_Link(struct Link* head)
{
int N;
struct Link* p=head;
puts("input your find node");
scanf("%d",&N);
while(p!=NULL){
if(p->date==N){
printf("Find node\n");
}
p=p->next;
}
}