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

Linear table of data structure

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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);}

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

Internet Technology

Wechat

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

12
Report