当前位置:网站首页>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
边栏推荐
- Use Base64 to show pictures
- AI: the Elephant in Room
- 陆奇首次出手投资量子计算
- MySQL optimistic lock and pessimistic lock
- J. Med. Chem. | RELATION: 一种基于靶标结构的深度学习全新药物设计模型
- ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext
- 太阳塔科技招聘PostgreSQL数据库工程师
- 全局快门和卷帘快门的区别
- Form repeated submission problem
- Precautions for map interface
猜你喜欢
随机推荐
Go 字符串比较
[wangdingbei 2020 Qinglong formation]areuserialz
Correspondence between three-tier architecture and SSM
Gesture recognition based on mediapipe
陆奇首次出手投资量子计算
Learn SCI thesis drawing skills (f)
Subscript operator of map
Install using snap in opencloudos NET 6
多线程习题
Lu Qi invests in quantum computing for the first time
swagger UI :%E2%80%8B
基于STM32设计的宠物投喂器
Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
2021-04-12 链表第一次实现!!!
[GXYCTF2019]BabyUpload
mysql乐观锁与悲观锁
高性能算力中心 — RDMA — 实现技术
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist
NFTs、Web3和元宇宙对数字营销意味着什么?
Opening, creating and storing files




![[極客大挑戰 2019]HardSQL](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)

![[MRCTF2020]Ez_bypass](/img/cd/bd6fe5dfc3f1942a9959a9dab9e7e0.png)

