Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sequence table of linear table in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about the C language linear table order table to share with you how to use content. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Linear Table--Sequential Table (C Language)

concept

A sequential representation of a linear table refers to the sequential storage of data elements in a linear table in a set of memory locations with consecutive addresses. This representation is also called a sequential storage structure or sequential mapping of a linear table. Usually, linear tables of this storage structure are called sequential lists. It is characterized by logically adjacent data elements whose physical order is also adjacent.

1. Storage structure of sequence table

#include #include #define MAX_SIZE 1000 //the maximum number that a sequence table can store typedef int DATA; //abstract int typedef struct _List{ DATA *data; int length;} SeqList;#include #include #define MAX_SIZE 1000 //Maximum number that a sequence table can store typedef int DATA; //abstract int typedef struct _List{ DATA *data; int length;} SeqList;2. Basic operations of sequence table//initialize sequence table SeqList* initList() { SeqList* list = malloc(sizeof(SeqList)); assert(list != NULL); list->data = malloc(MAX_SIZE * sizeof(DATA)); list->length = 0; //The initial length of the sequence table is 0 assert(list->data != NULL); return list;}2.1 Insertion of sequence tables

Head insertion:

1. Move all data elements back one place in a table

2. Insert data element at position 0

3. Length + 1

illustration

code

void insertByHead(SeqList* list, DATA data){ if (list->length >= MAX_SIZE){ printf("Sequence table is full and cannot insert data...\ n"); return; //jump out of the function } for (int i = list->length; i > 0; i--){ list->data[i] = list->data[i-1]; } list->data[0] = data; list->length++;}

insertion at specified position:

1. Move the index bit and all data elements after it one bit backward

2. Insert data element at index position

3. Length + 1

illustration

code

void insertByIndex(SeqList* list, int index, DATA data){ if ((index

< 0 || index >

= list->length) && list->length >= MAX_SIZE){ printf("The index value entered is incorrect or the sequence table is full. n"); return; //jump out of the function } for (int i = list->length; i > index; i--) { list->data[i] = list->data[i - 1]; } list->data[index] = data; list->length++;}

Tail insertion:

1. Insert data at length

2. Length + 1

illustration

code

void insertByTail(SeqList* list, DATA data){ if (list->length >= MAX_SIZE){ printf("Sequence table is full and cannot insert data...\ n"); return; //jump out of the function } list->data[list->length++] = data;}void insertByTail(SeqList* list, DATA data){ if (list->length >= MAX_SIZE){ printf("Sequence table is full and cannot insert data...\ n"); return; //jump out of the function } list->data[list->length++] = data;}

step

By comparing values one by one, if the same element exists in the table, the previous element position is returned.

code

int find(SeqList* list, DATA data){ for (int i = 0; i

< list->

length; i++) { if (list->data[i] == data) { return i; } } return -1;}2.3 Deletion of sequence tables

Delete element by value

1. Find the element index corresponding to the value

2. Move index + 1 and all elements behind it forward by one

3. Length - 1

illustration

code

void deleteByData(SeqList* list, DATA data) { if (list->length == 0){ printf("table is empty, no deletion required...\ n"); } int ret = find(list, data); if (ret != -1) { for (int i = ret; i

< list->

length - 1; i++) { list->data[i] = list->data[i + 1]; } list->length--; } else{ printf("This data does not exist, cannot delete...\ n"); }}

Delete elements by index

1. Move index + 1 and all elements behind it forward by one

2. Length - 1

illustration

code

void deleteByIndex(SeqList* list, int index) { if (list->length == 0){ printf("table is empty, no deletion required...\ n"); } if (index >= list->length){ printf("Index position greater than total length of table...\ n"); return; } for (int i = index; i

< list->

length - 1; i++) { list->data[i] = list->data[i + 1]; } list->length--;} Thank you for reading! About "C language linear table order table how to use" this article is shared here, I hope the above content can have some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report