Data structure-one-way linked list correlation algorithm

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW-2
#define OK 1
#define ERROR 0
typedef int ELEMTYPE;
//Unidirectional linked list structure
typedef struct LNODE {
Elemtype data;
struct Lnode *next;
}lnode,*linklist;

Linklist createlist_l (linklist l,int N);
void traverselist_l (linklist L);
int getelem_l (linklist l,int i,elemtype *e);
Linklist listinsert_l (linklist l,int i,elemtype e);
Linklist listdelete (linklist l,int i,elemtype *e);
Linklist mergelist_l (linklist la,linklist lb,linklist Lc);
locatelist_l (linklist l,elemtype e);
Linklist inverselist_l (linklist L);
int main ()
{
Linklist lin,lin1,lin2;
int n,e,i;
lin->next=null;

printf ("Please enter the number of elements:");
scanf ("%d", &n);
Lin = createlist_l (lin,n);

printf ("The element in the list is:");
Traverselist_l (Lin);

if (getelem_l (lin,1,&e))
printf ("The first element is:%d\n", e);

Lin = listinsert_l (lin,1,6);
printf ("The linked list after inserting an element is:");
Traverselist_l (Lin);

Lin = Listdelete (lin,1,&e);
printf ("The linked list after deleting an element is:");
Traverselist_l (Lin);
printf ("Deleted elements:%d\n", e);

printf ("Please enter the number of elements:");
scanf ("%d", &n);
Lin1 = createlist_l (lin1,n);

The combined list is:
printf ("Merged linked list:");
Lin2 = mergelist_l (lin,lin1,lin2);
traverselist_l (lin2);

The bit order to get an element is:
i = locatelist_l (lin2,3);
if (i = = 0) {
printf ("The element not found: \ n");
} else {
printf ("The element's bit order is:%d\n", i);
}
Reverse the one-way linked list
printf ("Inverted linked list is:");
Lin = inverselist_l (Lin);
Traverselist_l (Lin);
return 0;
}

//Create a one-way linked list
Linklist createlist_l (linklist l,int N) {
int i;
Linklist p;
L = (linklist) malloc (sizeof (Lnode));
L->next = NULL;
printf ("Please enter the value of the element:");
for (i=n; i>0;-I.) {
p = (linklist) malloc (sizeof (Lnode));
scanf ("%d", &p->data);
P->next = l->next;
L->next = p;
}
return L;
}
//Traverse linked list
void traverselist_l (linklist L) {
Linklist p;
p = l->next;
while (p) {
printf ("%d", p->data);
p = p->next;
}
printf ("\ n");
}

//Get the first element of the list
int getelem_l (linklist l,int i,elemtype *e) {
Linklist p;
Int J;
p = l->next;
j = 1;
while (P && j<i) {
p = p->next;
++j;
}
if (!p| | J>i) return ERROR;
*e = p->data;
return OK;
}

//Inserting an element into a linked list
Linklist listinsert_l (linklist l,int i,elemtype e) {
Linklist p,s;
Int J;
p = L;
j = 0;
while (P && j<i-1) {
p = p->next;
++j;
}
if (!p | | j>i-1) return ERROR;
s = (linklist) malloc (sizeof (Lnode));
S->data = e;
S->next = p->next;
P->next = s;
return L;
}

//Delete an element from a list
Linklist listdelete (linklist l,int i,elemtype *e) {
Linklist p,q;
Int J;
p = L;
j = 0;
while (P->next && j<i-1) {
++j;
p = p->next;
}
if (! ( P->next) | | J&GT;I-1) return ERROR;
Q = p->next;
P->next = q->next;
*e = q->data;
Free (q);
return L;
}

//Merging two linked lists into merge sort
Linklist mergelist_l (linklist la,linklist lb,linklist Lc) {
Linklist pa,pb,pc;
PA = la->next;
PB = lb->next;
Lc = PC = La;
while (PA && pb) {
if (Pa->data <= pb->data) {
Pc->next = PA;
PC = PA;
PA = pa->next;
} else {
Pc->next = PB;
PC = PB;
PB = pb->next;
}
}
Pc->next = PA?PA:PB;
Free (LB);
return Lc;
}

//Get a bit order for an element
int locatelist_l (linklist l,elemtype e) {
Linklist p;
int i;
p = l->next;
i = 0;
while (p) {
if (P->data = = e) {
++i;
Break
}
p = p->next;
}
if (p = = NULL) {
return 0;
} else {
return i;
}
}

//Reversing the unidirectional link list
Linklist inverselist_l (linklist L) {
Linklist Pre,cur,next;
Pre = l->next;
Cur = pre->next;
Next = cur->next;
Pre->next = NULL;
Cur->next = pre;
Pre = cur;
cur = next;
while (cur! = NULL) {
Next = cur->next;
Cur->next = pre;
Pre = cur;
cur = next;
}
L->next = pre;
return L;
}


Data structure-one-way linked list correlation algorithm

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.