Implementation of Simple Single-Chain tables in C ++

Source: Internet
Author: User

A linked list is a dynamic data structure that stores data elements in a group of any storage unit (continuous or discontinuous.
Each element in the linked list becomes a "Node", and each node is composed of a data field and a pointer field. The pointer field in each node points to the next node. Head is the "head Pointer", indicating the start of the linked list. It is used to point to the first node. the pointer field of the last pointer is null (empty address), indicating the end of the linked list.
It can be seen that the linked list structure can only be achieved through pointers, that is, a node must contain a pointer variable to store the address of the next node.
In fact, each node in the linked list can use several data and several pointers. The linked list with only one pointer in the node is called a single-chain table, which is the simplest chain table structure.

 

// Test1.cpp: defines the entry point for the console application <SPAN class = 'wp _ keywordlink '> Program </span>. <Br/> // </P> <p> # include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; </P> <p> typedef struct node <br/> {<br/> int data; <br/> struct node * Next; <br/>} node; </P> <p> void creatlist (node * & L) <br/> {<br/> node * s; <br/> int I; <br/> L = (node *) malloc (sizeof (node); <br/> L-> next = NULL; </P> <p> int N; <br/> cout <"Enter the number of nodes:" <Endl; <br/> CIN> N; <br/> cout <"Enter node data: "<Endl; <br/> for (I = 0; I <n; I ++) <Br/>{< br/> S = (node *) malloc (sizeof (node); <br/> int A; <br/> CIN>; <br/> S-> DATA = A; <br/> S-> next = L-> next; <br/> L-> next = s; <br/>}</P> <p >}< br/> void displaylist (node * l) <br/>{< br/> cout <"output: "<Endl; <br/> node * q; <br/> q = L-> next; <br/> for (; Q! = NULL; q = Q-> next) <br/>{< br/> cout <q-> data; <br/> cout <''; <br/> cout <Endl; </P> <p >}< br/> void main () <br/>{< br/> node * P; </P> <p> creatlist (p); <br/> displaylist (P ); </P> <p >}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.