当前位置:网站首页>2021-04-12 the first implementation of linked list!!!
2021-04-12 the first implementation of linked list!!!
2022-06-23 09:54:00 【Mr. Rabbit.】
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
typedef struct Node
{
DataType Data;
struct Node*Next;
}Node;
typedef struct Node *LinkList;
int InitList(LinkList *L) // Initialization of a single linked list with a header node
{
(*L) = (LinkList)malloc(sizeof(Node));
if (!L)
{
printf(" Failed to allocate memory !\n");
exit(0);
}
(*L)->Next = NULL;
return 0;
}
int ListEmpty(LinkList H)// It depends on whether the pointer field of the header node is empty ;
{
return H->Next==NULL;
}
void CreatFormHead(LinkList H)// The reverse ;
{
DataType data;// Build a data Type of data ;
Node*s;// Node pointer to be inserted ;
scanf("%d",&data);// Assign values to data fields ;
while(data!=-1)
{
s=(Node*)malloc(sizeof(Node));// Allocate space to the inserted node ;
s->Data=data;// Assign a value to the data field of the node ;
s->Next=H->Next;
H->Next=s;// The two most important steps of head interpolation ;
scanf("%d",&data);// Continue to assign ;
}
}
void CreatFormTail(LinkList H)// The order ;
{
Node*tail;
tail=H;
DataType data;// Build a data Type of data ;
Node*s;// Node pointer to be inserted ;
scanf("%d",&data);// Assign values to data fields ;
while(data!=-1)
{
s=(Node*)malloc(sizeof(Node));// Allocate space to the inserted node ;
s->Data=data;// Assign a value to the data field of the node ;
s->Next=tail->Next;
tail->Next=s;// The two most important steps of head interpolation ;
tail=s;//tail Always point to the tail ;
scanf("%d",&data);// Continue to assign ;
}
}
Node*Get(LinkList H,int i)
{
Node*p;// Node pointer ;
int j=0;
p=H;
if(ListEmpty(H))// Empty table ;
{
printf(" Table is empty !/n");
return 0;
}
while(!ListEmpty&&j<i)
{
p=p->Next;
j++;
}
if(j==0)
return p;
return NULL;
}
int Locate(LinkList H,DataType data)// According to the value lookup ;
{
Node*p=H->Next;
int i=1;
while(p)
{
while(p->Data!=data)
{
p=p->Next;
i++;
}
break;
}
return i;
}
int length(LinkList H)// Asked the table ;
{
Node*p;
p=H;
int len=0;
while(p->Next!=NULL)
{
len++;
p=p->Next;
}
return len;
}
void InsList(LinkList H,int i,DataType data)// The insert ;
{
Node*p;
Node*s;
p=H;
int j=0;
while(p->Next!=NULL&&j<i-1)
{
p=p->Next;
j++;
}
if(p==NULL)
{
return;
}
s=(Node*)malloc(sizeof(Node));
s->Data=data;
s->Next=p->Next;
p->Next=s;
}
int DelList(LinkList H,int i,DataType*data)// Delete operation ;
{
Node*p;
Node*s;
p=H;
int k=0;
while(i<0||i>length(H))
{
printf(" Illegal delete location !");
return 0;
}
if(p->Next!=NULL&&k<i-1)
{
p=p->Next;
k++;
}
s=p->Next;
*data=s->Data;
p->Next=s->Next;
free(s);
return *data;
}
void DestoryList(LinkList H)// Destroy the list ;
{
Node*p;
Node*q;
p=H;
while(p->Next!=NULL)
{
q=p;
p=p->Next;
free(q);
}
}
void PrintList(LinkList H)
{
Node*p;
p=H->Next;
while(p)
{
printf("%d",p->Data);
p=p->Next;
}
printf("\n");
}
int main()
{
LinkList(L);
LinkList(L1);
DataType data;
int num;// The sequence number of the element to be operated
int val;// Insert element values
InitList(&L);
InitList(&L1);
printf(" Head insertion method to build table (L1):\n");
CreatFormHead(L1);
printf(" The elements in the linked list are :\n");
PrintList(L1);
printf("\n");
printf(" The tail interpolation method is used to build the table (L):");
CreatFormTail(L);
printf(" The elements in the linked list are :\n");// The tail insertion method of the table created by
PrintList(L);
printf("\n");
printf(" Insert an element into the linked list :\n");
printf(" Please enter the insertion position :");
scanf("%d",&num);
printf(" Please enter the insertion element value :");
scanf("%d",&val);
InsList(L,num,val);
printf(" The elements in the linked list are :\n");
PrintList(L);
printf("\n");
printf(" Delete the elements in the linked list :\n");
printf(" Please enter the deletion location :");
scanf("%d",&num);
DelList(L,num,&data);
printf(" The value of the deleted element is %d\n",data);
printf("\n");
printf(" The elements in the linked list are :\n");
PrintList(L);
printf("\n");
printf(" The length of the list is :%d\n",length(L));
printf("\n");
printf(" Please enter the sequence number of the element you want to find :\n");
scanf("%d",&num);
Node*p=Get(L,num);
printf(" The first %d The element values are :%d\n",num,p->Data);
printf(" Please enter the element value to find :\n");
scanf("%d",&val);
printf("%d The sequence number of the position in the table is :%d\n",val,Locate(L,val));
printf("\n");
system("pause");
return 0;
}
Refer to Mr. caojidong
```c
Insert a code chip here
边栏推荐
- Distributed common interview questions
- 2022 Gdevops全球敏捷运维峰会-广州站精华回放(附ppt下载)
- Gesture recognition based on mediapipe
- [GXYCTF2019]BabySQli
- mysql innodb 的 redo log buffer 中未 commit 的事务持久化到 redo log 后,万一事务 rollback 了怎么办?redo log 怎么处理这个事务操作?
- sql编写问题,求出当月和上月的环比值
- After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
- Three methods to find the limit of univariate function -- lobida's rule and Taylor's formula
- regular expression
- 【CTF】bjdctf_2020_babyrop
猜你喜欢

Go 单元测试
![[ciscn2019 North China Day2 web1]hack world](/img/bf/51a24fd2f9f0e13dcd821b327b5a00.png)
[ciscn2019 North China Day2 web1]hack world

What is BFC? What problems can BFC solve

Correspondence between three-tier architecture and SSM

AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future

什么是BFC?BFC可以解决什么问题

Typora set up image upload service

Cloud native database Amazon RDS

Cesium loading orthophoto scheme

Go string comparison
随机推荐
Fill the pit for repvgg? In fact, it is the repoptimizer open source of repvgg2
RBtree
Neither rain nor sunshine
laravel8 beanstalk 使用说明
oracle中遇到的bug
Multithreaded exercises
135,137,138,445禁用导致无法远程连接数据库
高性能算力中心 — NVMe/NVMe-oF — NVMe-oF Overview
ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext
Three methods to find the limit of univariate function -- lobida's rule and Taylor's formula
regular expression
Install using snap in opencloudos NET 6
Go语言JSON 处理
Cesium加载正射影像方案
beanstalk 常用方法、说明
云原生数据库-Amazon RDS
J. Med. Chem. | Release: a new drug design model for deep learning based on target structure
2021-04-12 链表第一次实现!!!
135137138445 unable to remotely connect to the database due to disabling
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist