当前位置:网站首页>Sequence list and linked list
Sequence list and linked list
2022-07-29 04:16:00 【Stars and stars】
1. The linear table
The linear table (linear list) yes n A finite sequence of data elements with the same characteristics . Linear table is a kind of widely used in practice
Data structure used , Common linear tables : The order sheet 、 Linked list 、 Stack 、 queue 、 character string ...
A linear table is logically a linear structure , That is, a continuous straight line . But it is not necessarily continuous in physical structure , When a linear table is physically stored , It is usually stored in the form of array and chain structure .
2. The order sheet
2.1 Concept and structure
A sequence table is a linear structure in which data elements are sequentially stored in a section of storage cells with continuous physical addresses , Generally, array storage is used
Store . Add, delete, check and modify the data on the array
2.2 Realization
SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;// Pointer to dynamic array
int size;// The number of data
int capacity;// Capacity
}SL;
// initialization
void SLInit(SL* ps);
void SLCheckCapacity(SL* ps);
void SLPrint(SL* ps);
// Tail insertion / Deletion at the end
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
// Head insertion / Head deletion
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
// Insert delete anywhere
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
// Find and modify
int SLFind(SL* ps, SLDataType x);
void SLModify(SL* ps, int pos, SLDataType x);
SeqList.c
#include "SeqList.h"
void SLInit(SL* ps)
{
assert(ps);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
assert(ps);
// Check capacity space , Full capacity expansion
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : (ps->capacity * 2);
SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * newCapacity);
if (tmp == NULL)
{
perror("SLCheckCapacity::SL* tmp:");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newCapacity;
}
}
return;
}
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0;i < ps->size;i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLPushBack(SL* ps, SLDataType x)
{
//assert(ps);
//SLCheckCapacity(ps);
//ps->a[ps->size] = x;
//ps->size++;
SLInsert(ps, ps->size,x);
}
void SLPopBack(SL* ps)
{
//assert(ps);
//assert(ps->size > 0);
//ps->size--;
SLErase(ps, ps->size -1);
}
void SLPushFront(SL* ps, SLDataType x)
{
//assert(ps);
//SLCheckCapacity(ps);
//for (int end = ps->size;end > 0;end--)
//{
// ps->a[end] = ps->a[end - 1];
//}
//ps->a[0] = x;
//ps->size++;
SLInsert(ps, 0, x);
}
void SLPopFront(SL* ps)
{
//assert(ps->size > 0);
//assert(ps);
//for (int start = 0;start < ps->size - 1;start++)
//{
// ps->a[start] = ps->a[start + 1];
//}
//ps->size--;
SLErase(ps, 0);
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int end = ps->size;end > pos;end--)
{
ps->a[end] = ps->a[end - 1];
}
ps->a[pos] = x;
ps->size++;
}
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int start = pos;start < ps->size-1;start++)
{
ps->a[start] = ps->a[start + 1];
}
ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0;i < ps->size;ps++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
3. Linked list
3.1 The concept and structure of linked list
Concept : A linked list is a physical storage structure that is not continuous 、 Nonsequential storage structure , The logical order of data elements is through the linked list
Pointer link order in .
3.2 The classification of the list
In practice, the structure of linked list is very diverse , The combination of the following is 8 A linked list structure :
1. One way or two way
2. Take the lead or not
3. Cyclic or acyclic
In practice, we usually use two kinds of structures :
3.3 The realization of linked list
3.3.1 Headless + A one-way + Implementation of addition, deletion, query and modification of acyclic linked list
Slist.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
SLTNode* BuySListNode(SLTNode x);
// Printing of single linked list
void SListPrint(SLTNode* plist);
// Tail insertion of single linked list
void SListPushBack(SLTNode** plist, SLTDataType x);
// Single chain head plug in
void SListPushFront(SLTNode** plist, SLTDataType x);
// Delete the end of the single list
void SListPopBack(SLTNode** plist);
// Delete the single chain header
void SListPopFront(SLTNode** plist);
// Single linked list lookup
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
void SListInsertAfter(SLTNode* pos, SLTDataType x);
void SListEraseAfter(SLTNode* pos);
SList.c
#include"SList.h"
SLTNode* BuySListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SLTNode* plist)
{
while (plist != NULL)
{
printf("%d->", plist->data);
plist = plist->next;
}
printf("NULL\n");
}
void SListPushBack(SLTNode ** plist, SLTDataType x)
{
SLTNode* newnode = BuySListNode(x);
SLTNode* ptail = *plist;
if (*plist == NULL)
{
*plist = newnode;
}
else
{
while ((ptail)->next)
{
(ptail) = (ptail)->next;
}
(ptail)->next = newnode;
}
}
void SListPushFront(SLTNode** plist, SLTDataType x)
{
SLTNode* newnode = BuySListNode(x);
newnode->next = *plist;
*plist = newnode;
}
void SListPopBack(SLTNode** plist)
{
assert(*plist);
SLTNode* ptail = *plist;
// There is only one node
if ((*plist)->next == NULL)
{
free(*plist);
*plist = NULL;
return;
}
// Multiple nodes
while (ptail->next->next)
{
ptail = ptail->next;
}
free(ptail->next);
ptail->next = NULL;
}
void SListPopFront(SLTNode** plist)
{
assert(*plist);
SLTNode* phead = *plist;
*plist = (phead)->next;
free(phead);
phead = NULL;
}
SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListEraseAfter(SLTNode* pos)
{
assert(pos);
if (pos->next == NULL)
return;
SLTNode* del = pos->next;
pos->next = del->next;
free(del);
}
3.3.2 Take the lead + two-way + The implementation of adding, deleting, checking and modifying the circular linked list
List.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}LTNode;
LTNode* BuyListNode(LTDataType x);
LTNode* ListInit();
void ListPrint(LTNode* phead);
void ListPushBack(LTNode* phead, LTDataType x);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPopFront(LTNode* phead);
// stay pos Insert before position x
void ListInsert(LTNode* pos, LTDataType x);
// Delete pos The nodes of the location
void ListErase(LTNode* pos);
List.c
#include"List.h"
LTNode* BuyListNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("malloc fail:");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
LTNode* ListInit()
{
LTNode* phead = BuyListNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPrint(LTNode* phead)
{
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
//LTNode* newnode = BuyListNode(x);
//LTNode* tail = phead->prev;
//tail->next = newnode;
//newnode->prev = tail;
//newnode->next = phead;
//phead->prev = newnode;
ListInsert(phead, x);
}
void ListPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
//LTNode* newnode = BuyListNode(x);
//LTNode* next = phead->next;
//next->prev = newnode;
//newnode->next = next;
//phead->next = newnode;
//newnode->prev = phead;
ListInit(phead->next, x);
}
void ListPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
//LTNode* del = phead->prev;
//LTNode* pre = del->prev;
//pre->next = phead;
//phead->prev = pre;
//free(del);
ListErase(phead->prev);
}
void ListPopFront(LTNode* phead)
{
//assert(phead);
//assert(phead->next != phead);
//LTNode* del = phead->next;
//LTNode* next = del->next;
//free(del);
//phead->next = next;
//next->prev = phead;
ListErase(phead->next);
}
void ListInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = BuyListNode(x);
LTNode* pre = pos->prev;
pre->next = newnode;
newnode->prev = pre;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* pre = pos->prev;
LTNode* next = pos->next;
pre->next = next;
next->prev = pre;
free(pos);
}
4. Advantages and disadvantages of sequence list and chain list
advantage | shortcoming | |
The order sheet | 1. Support random access 2.cpu High cache hit rate | Low efficiency of head or middle insertion , Capacity expansion ( A certain degree of performance consumption , There may be some waste of space ) |
Linked list ( Take the lead + two-way + Circular linked list ) | 1. Insert delete anywhere O(1) 2. Apply for release on demand
| Subscript random access is not supported |
边栏推荐
- 11. Backup switch
- Some problems about pointers
- 一个公司的面试笔记
- C语言:结构体简单语法总结
- Pointer of pointer???...
- 不会就坚持66天吧 权重生成随机数
- Methods of using multiple deformations on an element
- The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
- How to write the filter conditions of data integration and what syntax to use? SQL syntax processing bizdate can not be
- Introduction and examples of parameters in Jenkins parametric construction
猜你喜欢
你真的会写Restful API吗?
小程序:区域滚动、下拉刷新、上拉加载更多
10. Fallback message
HC06 HC05 BT
Const char* and char*, string constants
Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
Applet: Area scrolling, pull-down refresh, pull-up load more
Install the laser of ROS_ scan_ Problems encountered in match library (I)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
不会就坚持61天吧 最短的单词编码
随机推荐
Model tuning, training model trick
10.回退消息
请问,在sql client中,执行insert into select from job时,如何单
Code or script to speed up the video playback of video websites
SQL server how to judge when the parameter received by the stored procedure is of type int?
Don't the JDBC SQL connector of the big guys Flink now support all databases, such as vertica?
When array is used as a function parameter, it is better to use the array size as a function parameter
pat A1041 Be Unique
[kvm] install KVM
Not for 60 days, magical dictionary
14.haproxy+keepalived负载均衡和高可用
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
Pointer variables -printf%d and%p meaning
Function pointer and callback function
flink-sql 如何设置 sql执行超时时间
(.*?) regular expression
为什么opengauss启动的时候这么多的unknown?
Can you really write restful API?
Shielding ODBC load balancing mode in gbase 8A special scenarios?