Implementation of C ++ single-chain table (1)

Source: Internet
Author: User

// There are a lot of online information about this. Since I didn't learn the data structure well before, I have to re-understand the data structure,
// The following describes how to create a C ++-based Single-chain table and its operations. It does not use a template, so it is suitable
// A person with a little c ++ knowledge, but it cannot be transplanted enough. If you have any bugs, please point them out. Or contact me if you have any questions.
// Made by Virgil (2009.2.8)
// MSN: The hangyu_628@hotmail.com)
# Include <iostream>
Using namespace STD;
Int n = 10;

Typedef struct Node
{
Char name [20];
Struct node * next;
} Node;

Node * Create (int n)
{
Node * H, * P, * s;
If (H = new node) = NULL)
{
Cout <"memory allocation failed" <Endl;
Exit (1 );
}
H-> name [0] = '/0 ';
H-> next = NULL;
P = h;
For (INT I = 0; I! = N; ++ I)
{
If (S = new node) = NULL)
{
Cout <"memory allocation failed" <Endl;
Exit (1 );
}
P-> next = s;
Cout <"Enter the" <I + 1 <"Personal Name :";
Cin> S-> name;
S-> next = NULL;
P = s;
}
Return h;
}

// Query the node
Node * search (const node * H, const char * name)
{
Node * P = H-> next;
For (INT I = 0; I! = N; ++ I)
{
If (strcmp (name, p-> name) = 0)
{
Return P;
}
P = p-> next;
}
Return P;
}

// Insert later
Void insert (node * P)
{
Node * s;
If (S = new node) = NULL)
{
Cout <"memory allocation failed" <Endl;
Exit (1 );
}
Cout <"Enter the name you want to insert :";
Cin> S-> name;
S-> next = p-> next;
P-> next = s;
++ N;
}
// Query forward nodes
Node * searchpre (node * head, const char * name)
{
Node * P = head-> next;
Node * q = p-> next;
For (INT I = 0; I! = N; ++ I)
{
If (Q! = NULL)
{
If (strcmp (Q-> name, name) = 0)
{
Return P;
}
Else
{
P = p-> next;
Q = p-> next;
}
}
}
Return P;
}
// Delete a node
Void Delete (node * P, node * q) // P indicates the deleted node pointer, and Q indicates the previous node pointer.
{
Q-> next = p-> next;
Delete P;
-- N;
};

// Display and release the memory...
Void display (node * H)
{
Node * P;
P = H-> next;
For (INT I = 0; I! = N; ++ I)
{
Cout <"no." <I + 1 <"Personal Name:" <p-> name <Endl;
Delete P;
P = p-> next;
}
Delete h;
}

Int main ()
{
Node * head, * p = 0, * q = 0;
Char STR [20];
Cout <"create node data as follows:" <Endl;
Head = create (N );
Cout <Endl;
// Display (head );
Cout <"the query node to be queried is :";
Cin> STR;
// Query the current node and insert it after
P = search (Head, STR );
// Cout <"insert node ...";
// Insert (P );
// Query the front node and delete the current node.
Q = searchpre (Head, STR );
// Insert (Q );
Delete (p, q );
// Cout <"the inserted node data is as follows:" <Endl;
Display (head );

Return 0;
}

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.