当前位置:网站首页>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
} 边栏推荐
- vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
- [proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
- 如何指定pig-register项目日志的输出路径
- Prometheus, incluxdb2.2 installation and flume_ Export download compile use
- Real MySQL interview questions (25) -- common group comparison scenarios
- 编址和编址单位
- PAT 乙等 1010 C语言
- Real MySQL interview questions (XXVI) -- didi 2020 written examination questions
- android Handler内存泄露 kotlin内存泄露处理
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
猜你喜欢

Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL

Wechat applet; AI intelligent dubbing assistant

MySQL面试真题(二十四)——行列互换

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

True MySQL interview question (XXII) -- condition screening and grouping screening after table connection

MySQL面试真题(二十三)——拼多多-球赛分析

Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method

【斯坦福计网CS144项目】Lab2: TCPReceiver

MySQL面试真题(二十七)——RFM分析法对用户进行分类

Prometheus, incluxdb2.2 installation and flume_ Export download compile use
随机推荐
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
Pat class B 1010 C language
工作积累-判断GPS是否打开
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
PAT 乙等 1014 C语言
PAT 乙等 1011 C语言
雷达图canvas
PAT 乙等 1022 D进制的A+B
Activity启动模式和生命周期实测结果
Work accumulation - judge whether GPS is on
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
jvm-04.对象的内存布局
HierarchyViewer工具找不到 HierarchyViewer位置
Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
Oracle exception
Leetcode topic resolution divide two integers
MySQL面试真题(二十六)——滴滴2020年笔试题