Single-chain table implementation in C and Java

Source: Internet
Author: User
Tags return tag testlink

1. Implementation of single linked list in C Language

Typedef struct node/* node definition */

{

Item;/* Data Field */

Node * Next;/* Chain Domain */

} Node, * pnode;

Node DT;

Typedef pnode list;

We will compare a single-chain table with int A = 3. node (alias) is equivalent to int, and DT is equivalent to a variable name (note that dt is not a pointer, only one variable, So we define * pnode alias as a pointer ). Pnode indicates the pointer data type, pointing to the first address of the struct, and the data type is node *. For example, typedef pnode list = typedef node * List

# Include "testlink. H "# include <stdio. h> # include <stdlib. h> # include <stdlib. h> typedef int item;/* define data item type * // typedef struct node * pnode;/* define node pointer */pnode next; /* Chain Domain */typedef struct node/* node definition */{item;/* data domain */node * Next;/* Chain Domain */} node, * pnode; typedef pnode list; List makeempty (list l) {L = (node *) malloc (sizeof (node); L-> item = 0; l-> next = NULL; return l;} // Insert the void insert (Item X, lis T l, list p) {list temp; temp = (list) malloc (sizeof (node); If (temp = NULL) Exit (0 ); temp-> item = x; temp-> next = p-> next; P-> next = temp; // note that the data type of next is node *} List reverse (list header) {list p, q; P = header; q = (list) header-> next; if (header-> next = NULL) return header; else {While (Q! = NULL) {header-> next = Q-> next; q-> next = P; P = Q; q = header-> next ;}} return P ;} int display (list header) {int size = 0; If (header = NULL) printf ("[% d]", header-> item ); else {list p = header; // non-pass address while (p-> next! = NULL) {printf ("[% d]", p-> item); P = p-> next; size ++ ;} printf ("[% d] \ n", p-> item);} return size;} int main () {list = NULL; List P; int I; list = makeempty (list); printf ("Empty linked list \ n"); P = List; for (I = 1; I <5; I ++) {insert (I * I, list, P); P = p-> next; printf ("the inserted value is % d New node \ n ", p-> item) ;}p = List; display (list); printf ("data output in reverse order:"); display (reverse (p); Return 0 ;}

2. Java single Linked List Implementation

public class Node {    int value;Node nextNode = null;public String toString(){return "["+this.value+"]";}}

Public class testlink {public static void main (string ARGs []) {testlink test = new testlink (); node node1 = new node (); node1.value = 1; node node2 = new node (); node2.value = 2; node node3 = new node (); node3.value = 3; node node4 = new node (); node4.value = 4; node node5 = new node (); node5.value = 5; test. add (node1); test. add (node2); test. add (node3); test. add (node4); test. add (node5); system. out. Print ("output from the original linked list:"); test. display (test. header); system. out. print ("output in reverse order of the linked list:"); test. header = test. reserve (test. header); test. display (test. header); system. out. print ("insert node in linked list:"); node node7 = new node (); node7.value = 7; test. insert (node7, 5); test. display (test. header);} private node header = NULL;/** the return value of the single-chain table insertion operation is the node insertion position */Public int insert (node P, int index) {node q = header; int tag = 0; while (Q! = NULL & tag <(index-1) {// point Q to the previous node to be inserted q = Q. nextnode; tag ++;} If (q = NULL) Return-1; if (index! = 0) {P. nextnode = Q. nextnode; q. nextnode = P;} else {P. nextnode = header; header = P;} return tag;}/** reverse output of a single-chain table */Public node reserve (node header) {node p, q; P = header; Q = header. nextnode; If (header. nextnode = NULL) return header; else {While (Q! = NULL) {header. nextnode = Q. nextnode; q. nextnode = P; P = Q; q = header. nextnode ;}} return P ;}/ ** Add a node */Public int add (node p) {If (header = NULL) {header = P; header. nextnode = NULL; return 0;} else {node q = header; int Index = 0; while (Q. nextnode! = NULL) {q = Q. nextnode; index ++;} Q. nextnode = P; Return Index ;}/ ** output single-chain table */Public int display (node header) {int size = 0; If (header = NULL) system. out. print (header. tostring (); else {node P = header; // non-pass address while (P. nextnode! = NULL) {system. out. print (P. tostring (); P = P. nextnode; size ++;} system. out. println (P. tostring () + "");} return size ;}}

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.