[Learning "Drip"-"Data Structure"-"single-chain table"] calculates the intermediate element and the last K element of a single-chain table.

Source: Internet
Author: User
/** Algorithm Function: creates a single-chain table and outputs the k-th element at the bottom of the single-chain table (note that the last element in the single-chain table is the last element in the single-chain table). It outputs the intermediate element of the single-chain table. * Function Description: * 1. getkthelem (): returns the k-th element to the order linked list. * 2. getmidelem (): finds the intermediate element of a single-chain table. * The Single-chain table in the algorithm does not take the lead node. * @ Author: xiaoq-ohmygirl * @ time: 2012-06-20 **/# include <stdio. h> # include <malloc. h> # include <stdlib. h> # define maxnode 5 typedef struct linknode {int data; linknode * Next;} linknode, * linklist; int initlinklist (linklist & L, int N) {L = (linklist) malloc (sizeof (linknode); If (! L) {return 0;} scanf ("% d", & L-> data); L-> next = NULL; linklist P; For (INT I = n-1; i> 0; I --) {P = (linklist) malloc (sizeof (linknode); scanf ("% d", & P-> data ); p-> next = L-> next; L-> next = P;} return 1;} linklist reverselinklist (linklist & L) {If (L = NULL | L-> next = NULL) {return l;} linklist node = reverselinklist (L-> next ); l-> next = L; L-> next = NULL; return node;} void visit (linknode * nod E) {If (node! = NULL) {printf ("% d->", node-> data) ;}else {printf ("null") ;}} void getnewline () {printf ("\ n") ;}// void traverselink (linklist L) {linknode * cur = L; getnewline (); While (cur! = NULL) {visit (cur); cur = cur-> next;} getnewline ();}/** find the last K nodes of a single-chain table, note that the last 0th elements are the tail pointer of a single-chain table. **/Linklist getkthelem (int K, const linklist L) {If (k <0) {return NULL;} linklist first = L, second = L; while (k --) {If (first-> next! = NULL) {First = first-> next;} else {return NULL ;}} while (first-> next! = NULL) {First = first-> next; second = Second-> next;} return second;}/** obtain the intermediate elements of a single linked list. If the single-linked list contains an odd number of elements, then, if the element in the center is an even number, take the first element. */Linklist getmidelem (const linklist L) {If (L = NULL) {return NULL;} linklist first = L; linklist second = L; while (first! = NULL & First-> next! = NULL) {First = first-> next; second = Second-> next;} return second;} Main () {linklist LA = NULL; initlinklist (LA, maxnode); traverselink (LA); linklist kth; For (INT I = 0; I <maxnode; I ++) {kth = getkthelem (I, La ); visit (KTH); printf ("\ n");} printf ("mid:"); linklist mid = getmidelem (LA); visit (MID ); system ("pause"); 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.