Data structure C language implementation--Linear link list

Source: Internet
Author: User

Declaration.h

#ifndef declaration_h_included#define declaration_h_included#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0# Define Infeasible-1#define overflow-2#define elemtype inttypedef elemtype* triplet;typedef int Status;typedef struct LNO        de{elemtype data; struct Lnode *next;} Lnode, *linklist; #endif//declaration_h_included
Function.h

#ifndef function_h_included#define function_h_includedvoid createlist_l (linklist *l, int n);//reverse-Enter the value of n elements, CV lead node single-linked list Lstatus listinsert_l (linklist *l, int i, elemtype e)///In the single-chain linear table of the lead node insert element before the first position estatus listdelete_l (linklist * L, int i, elemtype e);//In the single-chain linear table of the lead node, delete the element I and return its value by E to status getelem_l (linklist L, int i, Elemtype *e);//l is the head pointer of the single-linked list of the leading nodes// When the first element is present, its value is paid to E and returns OK, otherwise ErrorStatus mergelist_l (linklist *la, linklist *lb, linklist *LC) is returned;// Merges the LA and LB tables into the LC and arranges void printlist_l (Linklist L) in a non-descending sequence;//output single-linked list contents #endif//function_h_included
Function.c

#include <stdio.h> #include <stdlib.h> #include "declaration.h" void createlist_l (linklist *l, int n) {Sta        Tus i;        Linklist p;        *l= (linklist) malloc (sizeof (Lnode));          (*l)->next=null; First establish the lead node of the single-linked list;-> priority than * High do not miss out here the parentheses for (i=n; i>0; i--) {p= (linklist) malloc (sizeof (Lnode)                 );//Generate New node scanf ("%d", &p->data);   p->next= (*l)->next;  Insert to table header (* L)->next=p;  The data is inserted backwards into the table, that is, the last input number in the table header}}//createlist_lstatus listinsert_l (linklist *l, int i, elemtype e) {linklist        P=*l, S;        Elemtype j=0;                while (P && J < i-1) {p=p->next;        j + +;   } if (!p | | J >i-1) return ERROR;        I is less than 1 or greater than the table length plus 1 s= (linklist) malloc (sizeof (Lnode));        s->data=e;        s->next=p->next;      p->next=s; First Connect and insert return OK;} Status listdelete_l (linklist *l, int i, Elemtype *e) {linklist p=*l, q;        Elemtype j=0;                while (P->next && J < i-1) {p=p->next;        j + +; } if (! ( P->next) | |      J > I-1) return error;//the deletion position is unreasonable q=p->next;        p->next=q->next;        *e=q->data;        Free (q); return *e;}        Status getelem_l (linklist L, int i, Elemtype *e) {linklist p;        Elemtype j=1;        p=l->next;                while (P && j<i)//sequential lookup until p points to element I or P is empty {p=p->next;        j + +;      } if (!p | | j>i) return ERROR;        The first element does not exist *e=p->data; return *e;} getelem_l () Status mergelist_l (linklist *la, linklist *lb, linklist *lc) {//la and LB two list of data in non-descending order//merge La and LB tables to Lc        and by non-descending sequence linklist pa, Pb, PC;        int i=0,j=0;         Pa= (*la)->next;        pb= (*LB)->next;        printf ("%x%x", (int) PA, (int) PB); (*LC) = (linklist)malloc (sizeof (Lnode));</span> pc= (*LC);                        while ((int) PA && (int) pb) {if ((pa->data) <= (Pb->data)) {                        Pc->next =pa;                        PC=PA;                pa=pa->next;                        } else {pc->next=pb;                        PC=PB;                pb=pb->next;                }} while (PA) {pc->next=pa;//Inserts the remaining segment PC=PA;        pa=pa->next;                 } while (Pb) {pc->next=pb;         Pc=pb=null; } return OK;        mergelist_l () void printlist_l (linklist L) {linklist p;        for (P=l->next;p;p=p->next)//l is not assigned a value for the Header data field of the linked list printf ("%d", p->data); printf ("\ n");}
Main.c

#include <stdio.h> #include <stdlib.h> #include "declaration.h" #include "function.h" int main () {Elemtype        E        Linklist *la, *lb, *LC;        La= (linklist *) malloc (sizeof (linklist));        lb= (linklist *) malloc (sizeof (linklist));        lc= (linklist *) malloc (sizeof (linklist));        printf ("Create LA, Please enter 5 number:");        createlist_l (La, 5);        printf ("la=");        printlist_l (*la);        printf ("The 3rd number in the La table is%d\n", getelem_l (*la, 3, &e));        printf ("Delete the first number in the La table is%d\n", listdelete_l (LA, 1,&e));         printf ("After delete first member, la=");        printlist_l (*la);        printf ("Create lb, please enter 4 number:");        createlist_l (Lb, 4);        printf ("lb=");        printlist_l (*LB);        listinsert_l (Lb, 2, 3);        printf ("After insert 3, LB =");        printlist_l (*LB);        printf ("Mergelist function, lc=\n");               if (mergelist_l (La, Lb, Lc) ==ok) {printf ("Merget success\n"); Printlist_l (*LC);        } else printf ("Merget failed"); return 0;}
define level two pointer la,lb,lc, and pass them as arguments to the createlist_l () function, so that we can use the local function to

*la, *lb, *LC allocates storage space that does not disappear when the function call is ended. In c language, parameter passing is the way that values are passed: If the value of the variable is used as a parameter, a copy of the value is passed in. When a function has no return type, the value of the variable is only valid within the calling function; If you are sitting with a pointer or array name, passing the address of the variable, the same change in the calling function does not spread to the function, but the memory space represented by the address is assigned, and the change is permanent. Will not be released when the call is ended.

When implementing mergelist_l (Linklist *la, linklist *lb, linklist *lc), initially forget to assign the address space for *LC, make the PC a wild pointer, the result of the run-time program has been wasted a long time. Remember to initialize the pointer when it is in use.

Operation Result:





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.