当前位置:网站首页>Implementation of linear list linked list structure
Implementation of linear list linked list structure
2022-06-23 05:57:00 【Octopus bro】
Recently, I learned data structure , Practice the implementation of chain storage structure of linear table , New linked list , Calculate the length of the list , lookup , Insert , Delete the four functions of the node , The code is as follows :
#include "stdio.h"
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
typedef int bool;
typedef int ElementType;
typedef struct LNode * PtrToLNode;
struct LNode{
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode List;
typedef PtrToLNode Position;
// All of the following functions have header nodes by default
List MakeEmpty();// Create a new linked list of leading nodes
int Lenth(List L);// Calculate the length of the list
ElementType FindKth(List L, int K);// Find the... In the linked list K Elements and return values
Position Find(List L, ElementType X);// According to the value lookup , Find the value for X The node of
bool Insert(List L, ElementType X, int i);// Will be worth x The node insertion bit order of is i The location of
bool Delete(List L, int i);// Delete the bit sequence i The node of
void PrintLiner(List L);// Print linked list
int main()
{
List L = MakeEmpty();
while(1)
{
printf(" Please enter the list you want to match L Actions performed :\n");
printf("1. Calculate the length of the list \n2. Print linked list \n");
printf("3. Find No K Nodes and return values \n4. Find the value for K And return the location \n");
printf("5. In the ordinal position i The insertion value of the position is X The node of \n6. The deletion order is i The node of \n");
printf("0. Exit procedure \n");
int i;
scanf("%d",&i);
switch(i)
{
case 0:
exit(-1);// Exit the program when the parameter is negative
case 1:
printf(" The length of the list is :%d\n",Lenth(L));
printf("\n\n\n");
break;
case 2:
PrintLiner(L);
printf("\n\n\n");
break;
case 3:
printf(" Please enter the node sequence ( from 1 Start ):\n");
int K;
scanf("%d",&K);
if(FindKth(L,K) > 0)
printf(" The first %d The node value is :%d\n",K,FindKth(L,K));
else
printf(" The bit order does not exist \n");
printf("\n\n\n");
break;
case 4:
printf(" Please enter the value you want to find ( Greater than 0):\n");
int N;
scanf("%d",&N);
Position tmp;
tmp = Find(L,N);
if(tmp)
printf(" The node value is :%d\n",tmp->Data);//( Because the return is the address , Unable to detect , Therefore, whether the detection value is the value to be found )
printf("\n\n\n");
break;
case 5:
printf(" Please enter the position and value to insert :\n");
int pos,num;
scanf("%d %d",&pos, &num);
Insert(L,num,pos);
printf("\n\n\n");
break;
case 6:
printf(" Please enter the bit order to delete ( from 1 Start ):\n");
int del;
scanf("%d",&del);
Delete(L,del);
printf("\n\n\n");
break;
default:
break;
}
}
return 0;
}
List MakeEmpty()// Create a new linked list of leading nodes
{
List L = (List)malloc(sizeof(List));
L->Data = 0;// The value of the header node is 0
L->Next = NULL;// Address is empty
return L;
}
int Lenth(List L)
{
int cnt = 0;// Initialize counter , Because there are header nodes , So from 0 Start
Position p = L->Next;// Point to the first node ,L It's the head node
while(p)
{
p = p->Next;
cnt++;
}
return cnt;
}
ElementType FindKth(List L, int K)// Find the... In the linked list K Elements and return values
{
Position p = L;
int cnt = 0;// Initialize counter , Because there are header nodes , So from 0 Start , If there is no header node, start from 1 Start
while(p && cnt < K)
{
p = p->Next;
cnt++;
}
if(p && cnt == K)
return p->Data;
else
return -1;
}
Position Find(List L, ElementType X)// According to the value lookup , Find the value for X The node of
{
Position p = L,tmp;
while(p && p->Data != X)
{
p = p->Next;
}
if(p)
return p;
else
return NULL;
}
bool Insert(List L, ElementType X, int i)// Will be worth x The node insertion bit order of is i The location of
{
if(X <= 0)
{
printf(" The value entered is illegal \n");
return FALSE;
}
Position pre = L, tmp;
int cnt = 0;
while(pre && cnt < i - 1)// Want to insert bit order i The location of , We need to find i-1
{
pre = pre->Next;
cnt++;
}
if(pre && cnt == i - 1 )
{
tmp = (Position)malloc(sizeof(Position));// Apply for a new node
tmp->Data = X;// Assign values to nodes
tmp->Next = pre->Next;// take i-1 The following node address is assigned to the new node
pre->Next = tmp;// Insert the new node into i-1 after
return TRUE;
}
else
{
printf(" The inserted position does not exist \n");
return FALSE;
}
}
bool Delete(List L, int i)// Delete the bit sequence i( from 1 Start ) The node of
{
Position pre = L, tmp;
int cnt = 0;
while(pre && cnt < i - 1)// Cycle to the first i-1 Nodes
{
pre = pre->Next;
cnt++;
}
if(pre == NULL || cnt != i-1 || pre->Next == NULL)// If pre->Next It's empty , explain i-1 For the last node , The first i Nodes do not exist
{
printf(" The node to be deleted does not exist \n");
return FALSE;
}
else
{
tmp = pre->Next;
pre->Next = tmp->Next;
free(tmp);
return TRUE;
}
}
void PrintLiner(List L)// Print linked list
{
Position p = L->Next;// Because there are header nodes ,p Is the first node after the header node
while(p)
{
printf("%d ",p->Data);
p = p->Next;
}
printf("\n");// Line break
} 边栏推荐
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
- The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
- New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
- 技能自检 | 想当测试Leader,这6项技能你会吗?
- MySQL面试真题(二十四)——行列互换
- Pat class B 1012 C language
- Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
- MDM data cleaning function development description
- The digital collection market has just begun
- jvm-04. Object's memory layout
猜你喜欢

C primer plus learning notes - 2. Constant and formatted IO (input / output)

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

数字藏品火热背后需要强大的技术团队支持 北方技术团队

The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming

Wireshark TS | 视频 APP 无法播放问题

Behind the hot digital collections, a strong technical team is needed to support the northern technical team

The digital collection market has just begun
![[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code](/img/e2/24eb2a60e3dc603b3ec4bfefd0b8e5.png)
[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code

True MySQL interview question (21) - Finance - overdue loan

Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
随机推荐
PAT 乙等 1014 C语言
Addressing and addressing units
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
Raspberry pie assert preliminary exercise
True MySQL interview question (24) -- row column exchange
jvm-03.jvm内存模型
Pat class B 1010 C language
The digital collection market has just begun
Pat class B 1009 C language
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
Activity startup mode and life cycle measurement results
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
Wechat applet: future wife query generator
Pat class B 1011 C language
PAT 乙等 1023 组个最小数
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
What is the magic of digital collections? Which reliable teams are currently developing
Kotlin Android simple activity jump, simple combination of handler and thread