首页 > 其他分享 >单链表c语言实现网上查找

单链表c语言实现网上查找

时间:2022-10-23 22:22:05浏览次数:43  
标签:Node 单链 语言 int next LinkList 查找 printf LNode

插入

#include <malloc.h>
#define SIZE  100
#define  INCREMENT_SIZE 10
typedef struct  LNode
{
   int data;
   LNode *next;
}LNode,*LinkList;
 
//creat a LinkList
bool creatLinklist(LinkList&L,int n)
{
	LinkList p,q,t,s;
	L=(LNode*)malloc(sizeof(LNode));
	if(!L)
		return false;
	L->next=NULL;
	for(int i=n;i>0;i--)
	{
		p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p;
	}
	p=L->next;//The first Node
	q=p->next;//The second Node
	p->next=NULL; 
	while(q)
	{
		t=q->next;// keep the third Node
		q->next=p;// the second Node points to the first Node in a loop
		p=q;      //p points to next Node
		q=t;      //q points to next Node
	}
	L->next=p;    //set head Node
	return true;
}
 
bool LinkListInsert(LinkList&L,int i,int e)
{
	int j=0;
	LinkList p,s;
	p=L->next;
	while(p)
	{
		j++;
		if(j<i-1)
			p=p->next;
		else
			break;
	}
	if(j>i-1||!p)
	return false;
	s=(LNode*)malloc(sizeof(LNode));
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
 
bool LinklistDelete()
{
	return true;
}
void main()
{
	LinkList Llist,p;
	int k;
	int elemet;
	int position;
	printf("input the number of LinkList to be created:");
	scanf("%d",&k);
	creatLinklist(Llist,k);
	printf("\n");
	printf("input the position to insert in LinkList:");
	scanf("%d",&position);
	printf("\n");
	printf("input the element to insert in LinkList:");
	scanf("%d",&elemet);
	printf("\n");
	LinkListInsert(Llist,position,elemet);
	printf("out put the new LinkList:\n");
	p=Llist->next;
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
	free(Llist);
}

删除

#include <malloc.h>
#define SIZE  100
#define  INCREMENT_SIZE 10
typedef struct  LNode
{
   int data;
   LNode *next;
}LNode,*LinkList;
 
//creat a LinkList
bool creatLinklist(LinkList&L,int n)
{
	LinkList p,q,t,s;
	L=(LNode*)malloc(n*sizeof(LNode));
	if(!L)
		return false;
	L->next=NULL;
	for(int i=n;i>0;i--)
	{
		p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p;
	}
	p=L->next;//The first Node
	q=p->next;//The second Node
	p->next=NULL; 
	while(q)
	{
		t=q->next;// keep the third Node
		q->next=p;// the second Node points to the first Node in a loop
		p=q;      //p points to next Node
		q=t;      //q points to next Node
	}
	L->next=p;    //set head Node
	return true;
}
//delete a Node
bool LinklistDelete(LinkList&L,int i,int &e)
{
	LinkList p,q;
	int j=0;
	p=L->next;
	while(p)
	{
		j++;
		if(j<i-1)
			p=p->next;
		else
			break;
	}
	if(j>i-1||!p)
		return false;
	q=p->next;
	p->next=q->next;
	e=q->data;
	free(q);
	return true;
}
void main()
{
	LinkList Llist,p;
	int k;
	int elemet;
	int position;
	printf("input the number of LinkList to be created:");
	scanf("%d",&k);
	creatLinklist(Llist,k);
	printf("\n");
	printf("input the position to delete in LinkList:");
	scanf("%d",&position);
	printf("\n");
	LinklistDelete(Llist,position,elemet);
	printf("output the deleted data of Node:%d\n",elemet);
	printf("\n");
	printf("output the new LinkList:\n");
	p=Llist->next;
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
	free(Llist);
}

标签:Node,单链,语言,int,next,LinkList,查找,printf,LNode
From: https://www.cnblogs.com/LizhenGfdhh/p/16819813.html

相关文章

  • 查找算法
    总结常用的查找算法,针对不同的情况,能够反应出哪种数组结构是效率最快的##顺序查找条件:无序或有序队列。原理:按顺序比较每个元素,直到找到关键字为止。时间复杂度:O(n)#......
  • 单链表插入和删除一个节点的伪代码算法
    插入设ai-1节点为pai+1节点为q插入节点为t则p-->t-->next=q-->next删除设ai-1节点为pai+1节点为q删除的字节为tp-->next=t-->nextfree(t)参考链接https://bl......
  • C语言——自定义类型(结构体+枚举+联合)
    结构体基础知识结构是一些值的集合,这些值被称为成员变量;结构体可以存储不同类型的数据项,而数组中是存储相同类型数据项声明structtag{//struct是关键字,tag是结构体标签名......
  • C语言问题定位总结
    二分搜索法gitbisect命令,可以定位问题引入的第一个commit,如下图:用法举例:1、下载模拟代码:[email protected]:bradleyboy/bisectercise.git$cdbisecterci......
  • 嵌入式-C语言基础:指针
    指针就是地址,变量的值可以通过两种方式访问,一个是通过变量名,一个是通过地址访问。从而引出一个问题,即什么是指针变量?整型(字符)变量就是存放整形(字符)的变量,指针变量就是存放......
  • C语言_2
    video3~指的是按位取反。此时“位”值的还是二进制位;++k和k++的区别,前置++是先++再使用,后置++是先使用在++;强制类型转换:在想要转换类型的前面加上括号然后给与相应的类型名......
  • 【自然语言处理概述】百度百科数据爬取
    【自然语言处理概述】百度百科数据爬取作者简介:在校大学生一枚,华为云享专家,阿里云专家博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建......
  • 【自然语言处理概述】数据预处理
    【自然语言处理概述】数据预处理作者简介:在校大学生一枚,华为云享专家,阿里云专家博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建设专家......
  • C语言笔记基础知识
    ......
  • C语言的练习题
    有1,2,3,4四个数字,那能组成多少个互不相同且无重复数字的三位数?都是多少?分析:三位数可表示为:个位:g,十位:s,百位:b.可以有多少组合:用for语句的嵌套#include<stdio.h>intmain(......