Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Linear table of data structure

Shulou Source: shulou.com Published: 2022-06-02 19:31:59 10月13日 Update

I. Overview

The sequential representation of a linear table is characterized by the fact that the two adjacent elements in the logical relationship are also physically adjacent. The advantage of this data structure is that any element in the table can be read randomly; the disadvantage is that when inserting or deleting, a large number of elements need to be moved.

The opposite chain representation does not require the physical locations of logically connected elements to be adjacent, and each element not only stores its own data, but also stores information indicating the location of its subsequent elements. Therefore, there is no need to move a large number of elements when inserting and deleting, but it does not support random reading of any element in the table.

Second, the realization of linear linked list

# include typedef struct Node {int data; struct Node * next;} Node;int initLinkList (Node * node) {node- > data = 0; node- > next = NULL; return 0;} int getLinkListLen (Node * node) {int ibido; for (iTun0; node- > next! = NULL; iota +) {node = node- > next;} return I;} int getLinkListElm (Node * node, int num, int * data) {int ibido; int len Len = getLinkListLen (node); if (num > len) {printf ("the number exceed link list lenth\ n"); return-1;} for (iTun0; I next;} * data = node- > data; return 0;} int insertLinkList (Node * node, int num, int * data) {int iodized; int len; Node * newNode = (Node *) malloc (sizeof (Node)); newNode- > data = * data NewNode- > next = NULL; len = getLinkListLen (node); if (num > len) {printf ("the number exceed link list lenth\ n"); return-1;} for (iTun0; I next;} newNode- > next = node- > next; node- > next = newNode; return 0;} int delLinkList (Node * node, int num) {int ionomo; int len = 0 Node * delNode = (Node *) malloc (sizeof (Node)); len = getLinkListLen (node); if (num > len) {printf ("the number exceed link list lenth\ n"); return-1;} for (iTun0; I next;} delNode = node- > next; node- > next = delNode-> next; free (delNode); return 0;} void printLinkList (Node * node) {int ionom0; for (iTun0) Node- > next! = NULL; iTunes +) {printf ("% d", node- > next- > data); node = node- > next;} printf ("\ n");} int main (int argc, char argv []) {testInsertLinkList (); testDelLinkList (); testGetLinkListElm (); return 0;} void testInsertLinkList () {Node * node = (Node *) malloc (sizeof (Node)); initLinkList (node); int num = 8 InsertLinkList (node, 0, & num); printf ("the link list should be: 8, and it is:"); printLinkList (node); num = 9; insertLinkList (node, 0, & num); printf ("the link list should be: 9 8, and it is:"); printLinkList (node); num = 1; insertLinkList (node, 2, & num); printf ("the link list should be: 9 8 1, and it is:") PrintLinkList (node);} void testDelLinkList () {Node * node = (Node *) malloc (sizeof (Node)); initLinkList (node); int num = 8; insertLinkList (node, 0, & num); num = 9; insertLinkList (node, 0, & num); num = 122; insertLinkList (node, 1, & num); printf ("the link list should be: 9 1228, and it is:"); printLinkList (node) DelLinkList (node, 1); printf ("the link list should be: 1228, and it is:"); printLinkList (node);} void testGetLinkListElm () {int data = 0; Node * node = (Node *) malloc (sizeof (Node)); initLinkList (node); int num = 8; insertLinkList (node, 0, & num); getLinkListElm (node, 1, & data) Printf ("data should be 8, and it is:% d\ n", data); num = 9; insertLinkList (node, 1, & num); num = 10; insertLinkList (node, 2, & num); getLinkListElm (node, 3, & data); printf ("data should be 10, and it is:% d\ n", data);}

Tags: Elements location data linearity physics logic storage movement data structure structure two advantages information indications characteristics disadvantages order chain support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Xiaomi MariaDB macOS