Single-chain table operation (Data Structure Experiment 1)

Source: Internet
Author: User

Lab content

1. initialize a single-chain table with table header nodes.
2. Create a single-chain table with a table header node from the inserted node in the header. Set the element type in the table to integer, and input the element value from the keyboard.
3. Insert a node from the end of the table to create a single-chain table with a table header node. Set the element type in the table to integer, and input the element value from the keyboard.
4. Print a single-chain table with table header nodes.
5. Empty a single-chain table with table header nodes.


Code: (write the functions and provide the call data. modify the code according to the experiment requirements)

# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # define M 100 typedef int etype; // defines the type of the node value of the single-chain table as integer typedef struct node {etype data; // The data field struct node * link in the single-chain table; // pointer field of a single-chain table} node; typedef node * List; // defines a single-chain table list buildlist1 (); List buildlist2 (); void printlist (list first ); void clear (list * First); // create a linked list by using the header insertion method. List buildlist1 () {node * l; etype X, N; L = (node *) malloc (sizeof (node); // Request Header Node space L-> link = NULL; // Initialize an empty linked list printf ("Enter the number of elements: \ n"); scanf ("% d", & N); printf ("Enter the elements: \ n "); // X is the data in the Linked List data field for (INT I = 0; I <n; I ++) {scanf (" % d ", & X ); node * P; P = (node *) malloc (sizeof (node); // apply for a new node p-> DATA = x; // assign a value to the node data field p-> link = L-> link; // Insert the node to the header l --> | 2 | --> | 1 | --> null L-> link = P;} return l ;} // create the list buildlist2 () {node * l, * r; etype n, x; L = (node *) malloc (sizeof (node) by means of the End Plug-in )); // Request Header Node space L-> link = NULL; // Initialize an empty linked list printf ("Enter the number of elements: \ n"); scanf ("% d", & N); r = L; // R always points to the terminal node, start with head node printf ("Please input element: \ n"); For (INT I = 0; I <n; I ++) {scanf ("% d ", & X); node * P; P = (node *) malloc (sizeof (node); // apply for a new node p-> DATA = x; // assign a value to the node data field R-> link = P; // Insert the node to the header l --> | 1 | --> | 2 | --> null r = P ;} r-> link = NULL; return l;} // output linked list void printlist (list first) {node * Li; Li = first-> link; while (Li! = NULL) {printf ("% d", Li-> data); Li = Li-> link;} printf ("\ n ");} // clear the linked table void clear (list * First) {node * P = * First; while (* First) {P = (* First)-> link; free (* First); * First = P;} int main () {node * l; L = buildlist1 (); // call builtdlist1 (prefix) create a single-chain table algorithm printlist (l); // print a single-chain table clear (& L); // clear a single-chain table L = buildlist2 (); // call builtdlist2 (post-insertion) create a single-chain table algorithm printlist (l); // print a single-chain table clear (& L); // clear a single-chain table}


Single-chain table operation (Data Structure Experiment 1)

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.