Implement various operations of single linked list with C language (i) _c language

Source: Internet
Author: User
Recently, a new review of the data structure of the more important parts, now the results of their own record, is mainly modeled on the Min of the "Data Structure" (C language version), the examples and the following exercises for adaptation. First of all, is a list of various implementations, including some of the common knowledge points. For example, the inverse of single linked list, the merging of single linked list, and the realization of the algorithm of the middle node of single chain list are found.
The following is the definition of a single linked list structure:
Copy Code code as follows:

typedef struct LNODE
{
Elemtype data;
struct Lnode *next;
}linklist;


The following basic list of operations: Some macros, not given some of their definitions, can be passed, Min's "Data Structure" (C language edition), to see.
Copy Code code as follows:

/* Function: To build an empty lead node of the single linked list * *
Status initlist (struct Lnode **l)
{
(*l) = (struct Lnode *) malloc (sizeof (struct lnode)); Generating Header Node
if (!*l)
Exit (OVERFLOW);
(*l)->next = NULL;
return OK;
}
/* Destroy the Linear table * *
Status destroylist (struct Lnode *l)
{
struct Lnode *q;
while (L)
{
Q = l->next;
Free (L);
L = q;
}
return OK;
}
/* Reset L to an empty table * *
Status clearlist (struct Lnode *l)
{
Linklist *p,*q;
p = l->next;
while (p)
{
Q = p->next;
Free (p);
p = q;
}
L->next = NULL;
return OK;
}
/* To determine whether the linked list is an empty table * *
Status listempty (linklist *l)
{
if (L->next)
{
return FALSE;
}
Else
{
return TRUE;

}
}
/* Returns the number of elements in a single linked list.
int listlength (struct lnode *l)
{
int i=0;
linklist *p = l->next;
while (p)
{
i++;
p = p->next;
}
return i;
}
/L is the head pointer of a single linked table of the leading node, when the first element exists, its value is assigned to E, and it returns OK * *.
Status Getelem (struct lnode *l,int i,elemtype *e)
{
int j=1;
linklist *p = l->next;
while (P && j<i)
{
p = p->next;
j + +;
}
if (!p | | j>i)
return ERROR;
*e = p->data;
return OK;
}
/* Returns the bit order of the first Data element in L that satisfies the relationship compare ().
If the existence return value is 0,compare () is the decision function of the data element.
int Locateelem (struct lnode *l,elemtype e,status (*compare) (Elemtype,elemtype))
{
int i = 0;
linklist *p = l->next;
while (p)
{
i++;
if (compare (P->data,e))
return i;
p=p->next;
}
return 0;
}

Copy Code code as follows:

/* The cur_e is the data element in L, and give the first one, then return its predecessor with Pre_e.
Status Priorelem (struct lnode *l,elemtype cur_e,elemtype *pre_e)
{
Linklist *q,*p=l->next;
while (P->next)
{
Q = p->next;//q pointing to the successor of P
if (Q->data = = cur_e)
{
*pre_e = p->data;
return OK;
}
p = q;
}
return infeasible;

}
/* If cur_e is the data element in L and is not the last one, return its successor with Next_e.
Status Nextelem (struct lnode *l,elemtype cur_e,elemtype *next_e)
{
Linklist *p;
p = l->next;
while (P->next)
{
if (P->data = = cur_e)
{
* Next_e = p->next->data;
return OK;
}
p = p->next;
}
return infeasible;
}
/* Inserts element e*/before position I in single chain table L of the lead node
Status listinsert (struct lnode *l,int i,elemtype e)
{
int j = 0;
struct Lnode *p=l,*s=null;
while (P && j<i-1)
{
p=p->next;
j + +;
}
if (!p | | j>i-1)
return ERROR;
s = (struct Lnode *) malloc (sizeof (struct lnode));
if (!s)
printf ("malloc error~\n");
P->next = s;
S->data = e;
P->next = s;
S->next = p->next;
P->next = s;
S->next = NULL;
p = s;
return OK;
}
/* Delete the first element in the single linked list of the lead node and have e return its value.
Status listdelete (linklist *l,int i,elemtype *e)
{

Linklist *p=l,*q;

int j=0;
while (P->next && j< i-1)
{
p = p->next;
j + +;
}
if (!p->next | | j>i-1)
return ERROR;
Q = p->next;
P->next = q->next;
*e = q->data;
Free (q);
return OK;
}
/* Call VI () for each element of L, and print the OUTPUT statement * *
Status listtraverse (struct Lnode *l,void (*vi) (Elemtype))
{
linklist *p = l->next;
while (p)
{
VI (P-&GT;DATA);
p = p->next;
}
printf ("\ n");
return OK;
}

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.