当前位置:网站首页>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
边栏推荐
- High performance computing center RDMA implementation technology
- Subscript operator of map
- 基于mediapipe的手势数字识别
- Three implementation methods of distributed lock
- 全局快门和卷帘快门的区别
- Typora set up image upload service
- map的下标操作符
- 正则表达式
- After installing these five plug-ins, pycharm can be used for takeoff!
- 什么是BFC?BFC可以解决什么问题
猜你喜欢

ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext

Mysql database introduction summary

Cloud native database Amazon RDS

Qiming Xingchen Huadian big data quantum security innovation laboratory was unveiled and two black technology products were released

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

Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023

UEFI source code learning 3.7 - norflashdxe

Sequential representation and implementation of sequencelist -- linear structure
![[geek Challenge 2019] hardsql](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)
[geek Challenge 2019] hardsql

NFTs、Web3和元宇宙对数字营销意味着什么?
随机推荐
Cesium loading orthophoto scheme
[GXYCTF2019]BabyUpload
Alimentation des animaux de compagnie basée sur stm32
Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
Pizza ordering design - simple factory model
【CTF】bjdctf_ 2020_ babyrop
2021-04-12 链表第一次实现!!!
高性能算力中心 — NVMe/NVMe-oF — NVMe-oF Overview
[MRCTF2020]Ez_bypass
UEFI source code learning 3.7 - norflashdxe
mysql中innodb下的redo log什么时候开始执行check point落盘的?
CVPR大会现场纪念孙剑博士,最佳学生论文授予同济阿里,李飞飞获黄煦涛纪念奖...
beanstalk 常用方法、说明
oracle中遇到的bug
Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
Use Base64 to show pictures
J. Med. Chem. | Release: a new drug design model for deep learning based on target structure
The second Tencent light · public welfare innovation challenge was launched, and the three competition topics focused on the social value of sustainable development
16. system startup process
NFTs、Web3和元宇宙对数字营销意味着什么?