Data structure linked list and merge

Source: Internet
Author: User

Compared to the sequential table, linked lists enhance the connection between data, one by one, found July blog more deep, or lay a good foundation to understand the deeper layer of things.

or divide linklist.h,linklist.c,main.c

Before the rush to rush things, copy a large section of the change on the hand. Rewrite and find many problems in this piece.

LinkList.h

#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);


Linklist.c


#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));
		Pre-insertion Note if there are no elements then head node points to element if (temp = = NULL) {printf ("Insert fail");
	return 0; }else{for (linklist;linklist!= NULL; linklist = linklist, link) {if (linklist-link = = node) {Temp-&G
				T;info = element;
				linklist->link = temp;
			Temp->link = node;
	}}} return 1; The pre-inserted node is for us to pass our own note instead of writing the function}//Insert forward//Head node of the link header nodes have no value//actually the so-called post-insertion is the back plug but now node is the link list actually inserted time//So node's  Link is the first number of so-called "pre-interpolation" 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 back int 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 is 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 is 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 is not found");
	return 0; 
	} node = linklist; 
		while (node->link!= NULL && node->link-> info! = Element) {//link info is before element. 
	node = node-link; 
	printf ("test\n"), link->info first to determine that link is not empty.
	}//printf ("test2");
		if (node->link== NULL) {printf ("The element you deleted is 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 know where the node is not assigned there is no data structure is the data structure//originally empty malloc a node inserted in 
	Question two: One->link!= null this is the right while (one!= null)///When one is null and there is no link how to malloc new things are useless.
	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->link;
		One->link = temp;
	one = one->link;
	} return linken;  Linken Temporary pointing head node can return entire table}

Main.c


#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);
	}//finally completes the pre-insertion so that 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 does not perform if (Searchelement (Linklist1, 
	Element) = = NULL) printf ("Not found\n"); 
	
	else printf ("%d is the%dth element of the linklist\n", element,i); printf ("Input the valueof the element you delete\n ");
 	scanf ("%d", &element);
 	
Deleteelement (linklist1,element); 
	Printlist (LINKLIST1);
	*/Linklist3 = combine (LINKLIST1,LINKLIST2); 
	Printlist (LINKLIST3);
return 0;  }

One months has learned a little bit. Java's linklist was originally implemented, simple to use.


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.