C + + Reference and C pointers to create linked lists, binary tree troubles and differences

Source: Internet
Author: User


/*

* * Code features: Create a headache not a headache linked list, and then the specific data deleted.

* This time the theme is not in the code, mainly on the creation of the linked list when the issue of the argument, hey, don't believe you have not met

*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>//to allocate space and free up space
Define a linked list structure
typedef struct link{
int value;
struct link *next;
}*link,linkinstance;

/** if you want to pass in a parameter to create a linked list. As follows.
* * Created:
* * One,
void Create (LINK &lt) is equivalent to void Create (Linkinstance *&lt), using references. Reference "LT" to an alias equivalent to an argument, fully manipulating the argument
Called in the main function
LINK lt;
Create (LT);
Two, this should be very familiar
void Create (Linkinstance *lt)
Called in the main function
Linkinstance lt;
Create (&LT);

The result is this:


Wrong wording ***************************************************
* * A false notation is
void Create (LINK lt)
Called in the main function:
LINK lt;
Create (LT);
Why is wrong, because LT is just a copy.

This is your result: (haha)


* * For an example,
**void Test (int a), the above error example of LT is equivalent to a, so that the above is understood.
* * called in main
**int A;
**test (a);
* * This is ... No drip.
*/


void Create (link &lt) {//Initialize linked list, note "&lt"
link temp,current;//temp temporary space for adding nodes
lt = (link) malloc ( sizeof (struct link);//Create a head node
Lt->next = null;//short node
current = lt;
int value;
printf ("input data");
//Create node
for (int i = 0; i < 4; i++) {
temp = (link) malloc (sizeof (struct LINK));
if (temp = = NULL) {
printf ("Failed to allocate space");
Exit (0);
}//ifend
scanf_s ("%d", &value);
Temp->value = value;
Current->next = temp;
Current = current->next;
}//forend
Temp->next = null;//end, who knows what you get in the space.
}

Void del (link &lt,int val) {//delete list element
Link forth=lt;//Save the previous element, using the drop below
link current=lt->next;
while (current = NULL) {
if (Current->value = = val) {
Forth->next = current->next;// Previous element points to the next element , skip the middle of the Forth, current (Forth->next), Current->next
Free,//release the existing Space
}
else{
forth = current;
}
Current = forth->next;//points to the next element, which is replaced.
}//whileend
}
void Printlink (link lt) {//print
LINK L = lt->next;
while (L) {
printf ("%d\t", l->value);
L = l->next;
}
}
int _tmain (int argc, _tchar* argv[])
{
LINK lt;
Create (LT);//Created, LT is the head node, the head node does not save data
del (lt,3);//Delete all 3 of this element from the linked list.
Printlink (LT);//Print
GetChar ();
return 0;
}

/*
This is a detailed explanation of the citation written by an author: http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html
*/

C + + Reference and C pointers to create linked lists, binary tree troubles and differences

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.