Data structure--basic operation of single linked list (C language implementation)

Source: Internet
Author: User
data Structure--basic operation of single linked list (C language Implementation)

#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef int status;
typedef int ELEMTYPE;
typedef struct NODE
{
Elemtype data;
struct Node *next;
} lnode,*linklist;

void Build (Linklist L)//Create a single linked list of leading nodes
{
int n;
Linklist p,q;
P=l;
printf ("Please enter n and n data elements: \ n");
scanf ("%d", &n);
while (n--)
{
Q= (linklist) malloc (sizeof (Lnode));
scanf ("%d", &q->data);
q->next=null;
p->next=q;
p=q;
}
}
void Print (linklist L)//calculates the length of a single linked list, and then outputs a single linked list
{
int num=0;
Linklist p;
p=l->next;
while (p)
{
num++;
printf ("%d", p->data);
p=p->next;
}
printf ("\ n length is%d:\n", num);
}
void Tips ()
{
printf ("Select action by number key \ n");
printf ("<1> output single linked list and its length: \ n");
printf ("<2> lookup a direct precursor node of x: \ n");
printf ("<3> delete node with x value: \ n");
printf ("<4> invert elements in table: \ n");
printf ("<5> Delete all elements in the table that are greater than mink and less than maxk: \ n");
printf ("<6> Delete all extra elements of the same value in the table: \ n");
printf ("<7> decomposed into two lists: \ n");
printf ("<8> inserts a value of x in ascending list to make it still orderly: \ n");
printf ("<9> in ascending order: \ n");
printf ("<0> exit: \ n");
}
void Find (linklist l,int x)//lookup value x for direct precursor node q
{
Linklist p;
P=l;
while (P-&GT;NEXT&AMP;&AMP;P-&GT;NEXT-&GT;DATA!=X)
p=p->next;
if (P->next)
printf ("%d precursor node is:%d\n\n", x,p->data);
Else
printf ("Not Found!! \ n ');
}
void Delete (linklist l,int x)//delete a node with a value of X
{
Linklist p,q;
P=l;
while (P-&GT;NEXT&AMP;&AMP;P-&GT;NEXT-&GT;DATA!=X)
p=p->next;
if (P->next)
{
q=p->next;
p->next=q->next;
Free (q);
printf (delete succeeded!!) \ n ');
}
Else
printf ("No%d\n\n in the list", x);
}
void Nizhi (linklist L)//To reverse the elements in a one-way list, similar to the head interpolation method to establish a linked list!
{
Linklist p,s;
p=s=l->next;
l->next=null;
while (p)
{
s=s->next;
p->next=l->next;
l->next=p;
P=s;
}
printf ("Inverted success!!! \ n ');
}



void Delete1 (linklist L)//deletes all elements in the table that are greater than mink and less than maxk
{
int maxk,mink;
Linklist p,q,s;
printf ("Please enter mink,maxk:\n");
scanf ("%d%d", &mink,&maxk);
P=l;
while (P->next&&p->next->data<=mink)
p=p->next;
s=p->next;
while (S &&s->data<maxk)
{
Q=s;
s=s->next;
Free (q);
}
p->next=s;
printf ("delete successful \ n \ nthe");
}
void Delete2 (LINKLISTL)//deletes all extra elements of the table with the same values (so that all elements in the linear table after the operation have different values),
{
Linklist p,q,s;
P=l;
q=l->next;
while (Q->next)
{
if (Q->data==q->next->data)
{
p->next=q->next;
s=q;
q=q->next;
Free (s);
}
Else
{
p=p->next;
q=q->next;
}

}
printf ("Delete successful!!!! \ n ");
}
void Fenjie (LINKLISTL)//uses a linked list (1) to break it down into two lists, one of which is odd, and the other is even
{
Linklist S,P,LB,CUR1,CUR2;
lb= (linklist) malloc (sizeof (Lnode));
lb->next=null;
s=l->next;
l->next=null;
Cur1=l;
cur2=lb;
while (s)
{
P=s;
s=s->next;
p->next=null;
if (p->data&1)
{
cur1->next=p;
cur1=cur1->next;
}
Else
{
cur2->next=p;
cur2=cur2->next;
}
}
cur1=l->next;
cur2=lb->next;
printf ("element is odd-numbered list: \ n");
while (CUR1)
{
printf ("%d", cur1->data);
cur1=cur1->next;
}
printf ("\ n + element is an even-numbered list: \ n");
while (CUR2)
{
printf ("%d", cur2->data);
cur2=cur2->next;
}
printf ("\ n \ nthe");
}
void Insert (Linklist l,linklist p)//inserts the value x in the ascending list so that it is still ordered.
{
Linklist s;
S=l;
while (S->next&&s->next->data <p->data)
s=s->next;
p->next=s->next;
s->next=p;
Status Sort (linklist L)//in ascending order
{

Linklist s,r;
s=l->next;
l->next=null;
while (s)
{
R=s;
s=s->next;
r->next=null;
Insert (L,R);
}
return OK;
}
int main ()
{
int Op,x,flag;
Linklist l,p;
L= (linklist) malloc (sizeof (Lnode));
l->next=null;
l->data=-1;
Build (L);
Tips ();
scanf ("%d", &op);
while (OP)
{
Switch (OP)
{
Case 1:
Print (L);
Break
Case 2:
printf ("Please enter the element to find x:\n");
scanf ("%d", &x);
Find (L,X);
Break
Case 3:
printf ("Please enter the deletion x:\n to find");
scanf ("%d", &x);
Delete (L,X);
Break
Case 4:
Nizhi (L);
Break
Case 5:
Delete1 (L);
Break
Case 6:
Delete2 (L);
Break
Case 7:
Fenjie (L);
Break
Case 8:
printf ("Please enter the element to be inserted x:\n");
scanf ("%d", &x);
p= (linklist) malloc (sizeof (Lnode));
p->data=x;
Insert (L,P);
printf ("Insert succeeded!!! \ n ');
Break
Case 9:
Flag=sort (L);
if (flag)
printf ("Sort success!!") \ n ");
Break
}
Tips ();
scanf ("%d", &op);
}
return 0;
}

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.