NEFU锐格实验八[链表1-动态节点]
知识点
题目 | 知识点 |
---|---|
5825 | 查找前驱 |
5826 | 查询变形 |
5827 | 删除 |
5828 | 链表插入排序 |
5829 | 链表插入 |
题目
search操作的各种变形
5825
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H ,int x)//查询值
{
Lnode * p, * pre;//pre为前驱
int idx=1;
p=H->next;
while(p!=NULL&&p->data!=x)
{
++idx;
pre=p;
p=p->next;
}
if(p==NULL)printf("x不存在\n");
else
{
if(idx==1)printf("没有前驱\n");
else printf("%d\n",pre->data);
}
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
CreatList_Tail(H,n);
scanf("%d",&x);
SearchList(H,x);
}
return 0;
}
5826
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H )//查询
{
Lnode * p;
p=H->next;
int cnt=0;
while(p!=NULL)
{
if(p->data%2==0)cnt++;
p=p->next;
}
printf("%d\n",cnt);
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
CreatList_Head(H,n);
SearchList(H);
}
return 0;
}
5827
并不那么正规的remove操作(flag标记是否输出)
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
bool flag;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->flag=1;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
if(p->flag)printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H )//查询
{
Lnode * p;
p=H->next;
int cnt=0;
while(p!=NULL)
{
if(p->data%2==0)cnt++;
p=p->next;
}
printf("%d\n",cnt);
}
void RemoveList(Lnode * H,int x)//删除
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
if(p->data==x)p->flag=0;
p=p->next;
}
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
CreatList_Tail(H,n);
scanf("%d",&x);
RemoveList(H,x);
PrintList(H);
printf("\n");
}
return 0;
}
正规点的
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H )//查询
{
Lnode * p;
p=H->next;
int cnt=0;
while(p!=NULL)
{
if(p->data%2==0)cnt++;
p=p->next;
}
printf("%d\n",cnt);
}
void RemoveList(Lnode * H,int x)//删除
{
Lnode * p,* pre,*tmp;
pre=H;p=H->next;
while(p!=NULL)
{
if(p->data==x)
{
tmp=p;//直接用p释放后影响后续循环
pre->next=p->next;
p=p->next;//pre不用更新,p更新为下一个节点
free(tmp);//释放
}
else
{
pre=p;
p=p->next;
}
}
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
CreatList_Tail(H,n);
scanf("%d",&x);
RemoveList(H,x);
PrintList(H);
printf("\n");
}
return 0;
}
5828
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H )//查询
{
Lnode * p;
p=H->next;
int cnt=0;
while(p!=NULL)
{
if(p->data%2==0)cnt++;
p=p->next;
}
printf("%d\n",cnt);
}
void InsertList(Lnode * H,int x)//插入
{
Lnode * p,* pre,* idx;
pre=H;p=H->next;
idx=(Lnode *)malloc(sizeof (Lnode));
idx->data=x;
while(p!=NULL&&p->data<x)
{
pre=p;
p=p->next;
}
idx->next=p;
pre->next=idx;
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
CreatList_Tail(H,n);
scanf("%d",&x);
InsertList(H,x);
PrintList(H);
printf("\n");
}
return 0;
}
5829
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node * next;
}Lnode;
void CreatList_Tail(Lnode * H,int n)//尾插法
{
Lnode * p,*r;
r=H;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
r->next=p;r=p;
}
r->next=NULL;
}
void CreatList_Head(Lnode * H,int n)//头插法
{
Lnode *p;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
p=(Lnode *)malloc(sizeof (Lnode));
p->data=x;
p->next=H->next;
H->next=p;
}
}
void PrintList(Lnode * H)//打印
{
Lnode * p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void SearchList(Lnode * H )//查询
{
Lnode * p;
p=H->next;
int cnt=0;
while(p!=NULL)
{
if(p->data%2==0)cnt++;
p=p->next;
}
printf("%d\n",cnt);
}
void InsertList(Lnode * H,int x)//插入
{
Lnode * p,* pre,* idx;
pre=H;p=H->next;
idx=(Lnode *)malloc(sizeof (Lnode));
idx->data=x;
while(p!=NULL&&p->data<x)
{
pre=p;
p=p->next;
}
idx->next=p;
pre->next=idx;
}
int main()
{
Lnode * H;
int n,x;
while(~scanf("%d",&n))
{
H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
InsertList(H,x);
}
PrintList(H);
printf("\n");
}
return 0;
}