• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

单链表,删除小于x的所有元素

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;
void PrintList(LinkList L);
LinkList init(int n)
{
	int i=0;
	LinkList L;
	LNode *p,*q;
	L=(LinkList)malloc(sizeof(LNode));
	L->next=NULL;
	p=L;
	while(i<n)
	{
		q=(LinkList)malloc(sizeof(LNode));
		scanf("%d",&q->data);
		q->next=NULL;
		p->next=q;
		p=q;
		i++;
	}
	return L;
}
void PrintList(LinkList L)
{
    LNode *p;
	p=L->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}
void DeleteListLessThanX(LinkList L,ElemType x)
{
    LNode *p,*q;
    p=L;
    while(p!=NULL)
    {
        if(p->data<x)
        {
			q=p->next;
			p->data=q->data;
			p->next=q->next;
        }
        p=p->next;
    }
}
int main()
{
    int n,x;
    LinkList L;
	printf("链表元素数量n=");
    scanf("%d",&n);
	printf("开始输入链表元素:");
    L=init(n);
	PrintList(L);
	printf("删除小于x的值,x=");
    scanf("%d",&x);
    DeleteListLessThanX(L,x);
	printf("数据展示");
    PrintList(L);
    return 0;
}

输出

 输入

6
1 2 3 4 5 6
3

posted on 2022-04-12 20:02  王陈锋  阅读(119)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3