Data Structure linked list and merge

Source: Internet
Author: User

Compared with the sequence table, the linked list enhances the connection between data. One by one, we can find that July's blog is deeper, so we can lay a solid foundation to learn more about it.

Link List. h, LinkList. c, main. c

When I was in a hurry to catch up, I just handed in a copy of a large segment of changes. After rewriting, I found many problems in this section.

LinkList. h

 

<SPAN style="FONT-SIZE: 18px">#include<stdio.h>#include<malloc.h>typedef int DataType;typedef struct SimpleLinkList *Node;struct SimpleLinkList{DataType info;Node link;} ;typedef struct SimpleLinkList *LinkList;LinkList CreateNUllList();int isNull(LinkList linklist);int insertPost(LinkList linklist,Node node,DataType element);int insertBack(LinkList linklist,Node node,DataType element);int printList(LinkList linklist);Node searchElement(LinkList linklist,DataType element);DataType showValue(LinkList linklist,Node node);int deleteElement(LinkList linklist,DataType element);LinkList combine(LinkList one,LinkList another);</SPAN>
<SPAN style = "FONT-SIZE: 18px"> # include "LinkList. h "LinkList CreateNUllList () {LinkList linklist = (LinkList) malloc (sizeof (struct SimpleLinkList); if (linklist! = NULL) linklist-> link = NULL; else printf ("create fail! "); Return linklist;} int isNull (LinkList linklist) {return (linklist! = NULL); // not null return 1} int insertPost (LinkList linklist, Node node, DataType element) {Node temp = (Node) malloc (sizeof (struct SimpleLinkList )); // forward insert NOTE: if no element exists, point the header to element if (temp = NULL) {printf ("insert fail"); return 0 ;}else {for (linklist; linklist! = NULL; linklist = linklist-> link) {if (linklist-> link = node) {temp-> info = element; linklist-> link = temp; temp-> link = node ;}} return 1; // The forward insertion node requires us to pass the parameter but not write the function as} // insert forward // The head node of the linked list node of the leading node has no value // In fact, the so-called post insertion is post insertion the current node is actually inserted in the linked list // so the link of the node is the first number, which is called "Forward insertion" int insertBack (LinkList linklist, node node, DataType element) {Node temp = (Node) malloc (sizeof (struct SimpleLinkList); if (temp = NULL) {printf ("insert element fail! "); Return 0;} temp-> info = element; temp-> link = node-> link; node-> link = temp; return 1 ;} // insert backint printList (LinkList linklist) {if (linklist = NULL) {printf ("it is null"); return 0 ;}while (linklist-> link! = NULL) {printf ("% 2d", linklist-> link-> info); linklist = linklist-> link;} printf ("\ n"); return 1 ;} node searchElement (LinkList linklist, DataType element) {Node node; if (linklist = NULL) {printf ("the element was not found"); return NULL ;} node = linklist-> link; while (node! = NULL & node-> info! = Element) {node = node-> link;} if (node = NULL) {return NULL;} return node;} DataType showValue (LinkList linklist, Node node) {Node temp; if (linklist = NULL) {printf ("the element was not found"); return NULL;} temp = linklist-> link; while (temp! = NULL & temp-> link! = Node) {temp = temp-> link;} return node;} int deleteElement (LinkList linklist, DataType element) {Node node; Node temp; if (linklist = NULL) {printf ("the element you deleted was not found"); return 0 ;}node = linklist; while (node-> link! = NULL & node-> link-> info! = Element) {// The link info is before the element. Node = node-> link; // printf ("test \ n"); link-> info first, you must judge that the link is not empty!} // Printf ("test2"); if (node-> link = NULL) {printf ("the element you deleted was not found \ n"); return 0 ;} else {temp = node-> link; node-> link = temp-> link; free (temp); printf ("the element is deleted \ n "); return 1 ;}} LinkList combine (LinkList one, LinkList another) {// Finally, you know where the error is. If the node is not allocated, there is no data structure. The data structure is. // malloc, which is originally empty, is inserted into a node. // Question 2: one-> link! = NULL this is correct while (one! = NULL) // when one is null, there will be no link. It is useless to malloc new things. Node temp; LinkList linken; linken = one; while (one-> link! = NULL) one = one-> link; for (another; another-> link! = NULL; another = another-> link) {temp = (Node) malloc (sizeof (struct SimpleLinkList); temp-> info = another-> link-> info; temp-> link = another-> link; one-> link = temp; one = one-> link;} return linken; // linken can temporarily point to the header node to return the entire table} </SPAN>
<SPAN style = "FONT-SIZE: 18px"> # include "LinkList. h "int main () {int I, element; LinkList linklist1 = CreateNUllList (); LinkList linklist2 = CreateNUllList (); LinkList linklist3 = CreateNUllList (); Node note = linklist1; node note2 = linklist1; // if (note! = NULL) printf ("yes"); for (I = 0; I <10; I ++) {while (note! = NULL) note = note-> link; insertPost (linklist1, note, I);} // The forward insertion is completed to make for (I = 0; I <10; I ++) {insertBack (linklist2, linklist2, I);} // printf ("% d", linklist-> link-> info); printList (linklist1); printList (linklist2 ); /* printf ("input the value of the element you search \ n"); scanf ("% d", & element); if (searchElement (linklist1, element) = NULL) printf ("not found"); for (I = 0; note2! = SearchElement (linklist1, element); note2 = note2-> link, I ++); // NULL statement not executed if (searchElement (linklist1, element) = NULL) printf ("not found \ n"); elseprintf ("% d is the % dth element of the linklist \ n", element, I ); printf ("input the value of the element you delete \ n"); // scanf ("% d", & element); // deleteElement (linklist1, element ); // printList (linklist1); */linklist3 = combine (linklist1, linklist2); printList (linklist3); return 0 ;}</SPAN>

 

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.