/** 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 ;}