Simple C ++ Linked List Operation

Source: Internet
Author: User

// Linktabletest. cpp: defines the entry point of the console application. <Br/> // <br/> # include "stdafx. H "<br/> # include <iostream>; <br/> using namespace STD; <br/> // student structure, used to build a linked list <br/> struct student <br/> {<br/> // string name; <br/> char name [20]; <br/> int age; <br/> Student * nextstu; <br/>}; <br/> // program entry <br/> void main () <br/>{< br/> Student C = {"end", 0, null }; <br/> Student B = {"Lucy", 21, & c }; <br/> Student A = {"andlu", 23, & B}; <br/> Student * head = & A; <br/> Student * poi Nter = head; <br/> // function declaration <br/> int getlength (student * pointer, int length); <br/> void insertonenode (student * head, student * pointer, int Leng, int index, student * newnode); <br/> void showallnode (student * pointer); <br/> bool findonenodebyname (student * head, char * destname, bool inisfind); <br/> // perform Bubble Sorting on the array (TEST) <br/> void sortarray (INT arr [], int arrlen ); <br/> // function declaration ends </P> <p> // call the function to display all student information <br/> pointer = Head; <br/> showallnode (pointer); <br/> // obtain the length of the linked list <br/> pointer = head; <br/> int Leng = 0; <br/> Leng = getlength (pointer, Leng); <br/> cout <"result receiving chain table length:" <Leng <Endl; <br/> // search for Lucy <br/> pointer = head; <br/> bool isfindlucy = false; <br/> isfindlucy = findonenodebyname (Head, "Lucy ", isfindlucy); <br/> cout <"search result reception LUCY:" <isfindlucy <Endl; <br/> // insert a node <br/> head = & A; <br/> pointer = head; <br/> Student x = {"xmen", 0, null}; <br/> Student * xmen = & X; <br/> insertonenode (Head, pointer, Leng, 2, xmen); <br/> // call the function to display all student information <br/> pointer = head; <br/> showallnode (pointer ); <br/> // sort int arr <br/> int arr [10] = {,}; <br/> sortarray (ARR, 10); <br/> int I; <br/> CIN> I; </P> <p >}< br/>/** <br/> * Function Description: Insert a node. <br/> * parm: Head, header node index, behind the location to be inserted, behind the index <br/> */<br/> void inserto Nenode (student * head, student * pointer, int Leng, int index, student * newnode) <br/>{< br/> // int Leng = 0; <br/> // Leng = getlength (pointer, Leng); <br/> If (index> Leng) <br/>{< br/> cout <"sorry, the position you inserted exceeds the length of the linked list! "<Endl; <br/> return; <br/>} else if (Index = 0) <br/>{ <br/> cout <"you have inserted a header node! "<Endl; <br/> Student * temp; <br/> temp = head; <br/> newnode-> nextstu = temp; <br/> head = newnode; <br/> return; <br/>}< br/> for (INT I = 0; I <index-1; I ++) <br/>{< br/> pointer = pointer-> nextstu; <br/>}< br/> newnode-> nextstu = pointer-> nextstu; <br/> pointer-> nextstu = newnode; <br/>}< br/>/** <br/> * function description: get the length of the linked list <br/> * Parme: Head header node <br/> * Param: length returned length value <br/> */<br/> int getlength (Student * pointer, int length) <br/>{< br/> length = 0; <br/> while (pointer) <br/>{< br/> If (pointer-> nextstu! = NULL) <br/>{< br/> length ++; <br/> pointer = pointer-> nextstu; <br/>}< br/> else <br/>{< br/> cout <"chain table length:" <length <Endl; <br/> return length; <br/>}< br/>/** <br/> * function description: traverse all nodes <br/> * Param: pointer linked list header node <br/> * function: input the linked list header node, print the names of all nodes in the linked list <br/> */<br/> void showallnode (student * pointer) <br/>{< br/> while (pointer) <br/> {<br/> cout <pointer-> name <Endl; <br/> pointer = pointer-> Nextstu; <br/>}< br/>/* <br/> * check whether a student name exists in the linked list. <br/> * Param: <br/> * head: head node of the linked list <br/> * destname: name of the student to be searched <br/> * inisfind: returned result, <br/> */<br/> bool findonenodebyname (student * head, char * destname, bool inisfind) found? <br/> {<br/> inisfind = false; <br/> while (head) <br/> {<br/> If (! Strcmp (destname, head-> name) <br/>{< br/> inisfind = true; <br/> cout <"find what you are looking: "<inisfind <Endl; <br/> return inisfind; <br/>}< br/> head = head-> nextstu; <br/>}< br/> return inisfind; <br/>}< br/> // sort the linked list <br/> void sortstu () <br/>{</P> <p >}< br/> // perform Bubble Sorting on the array (TEST) <br/> void sortarray (INT arr [], int arrlen) <br/>{< br/> int flag, I, j, temp; <br/> for (I = 0; I <arrlen; I ++) <br/> {<br/> flag = 0; <br/> for (j = 0; j <arrlen-I-1; j ++) <br/>{< br/> If (ARR [J]> arr [J + 1]) <br/>{< br/> flag = 1; <br/> temp = arr [J]; <br/> arr [J] = arr [J + 1]; <br/> arr [J + 1] = temp; <br/>}< br/> If (flag = 0) <br/>{< br/> break; <br/>}< br/> // println all data <br/> for (I = 0; I <arrlen; I ++) <br/> cout <arr [I] <"->" <Endl; <br/>}

Related Article

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.