Data structure linked list operation set (build, Traverse, insert, delete, sort, length, empty judgment, etc.)

Source: Internet
Author: User
Tags clear screen

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


typedef struct NODE
{
int data;//data field
struct Node * pnext;//pointer field
}node, * pnode; node is equivalent to struct node, pnode equivalent to struct node *


function declaration
void Create_list (Pnode phead);//create a dynamic linked list
void Traverse_list (Pnode phead);//Output list
BOOL Is_empty (Pnode phead);//Judging the list is not empty
void Length_list (Pnode phead);//Find chain table length
void Insert_list (Pnode phead);//Insert Node
void Delete_list (Pnode phead);//Delete node
void Sort_list (Pnode phead);//list element ordering
void Play_choose (void);//Display menu bar
                         ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                                            ,         &N Bsp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                 &NBSP;                           
int main ()
{
int i;
Pnode Phead;
Phead= (pnode) malloc (sizeof (NODE));
if (phead==null)
{
printf ("Dynamic memory allocation failed!") ");
Exit (-1);
}
Play_choose ();
while (true)
{
printf ("\n\t\t, select the action you want to perform:");
scanf ("%d", &i);
Switch (i)
{
Case 1: Create_list (Phead);
Case 2: Traverse_list (Phead);
Case 3: Insert_list (Phead);
Case 4: Delete_list (Phead);
Case 5:Sort_list (Phead);
Case 6:play_choose ();p rintf ("\t\t\t\t-processed linked list: \ n"); Traverse_list (phead); break;
Case 7:length_list (Phead);
Case 8: Exit (-1);
Default
printf ("You entered an illegal value \ n \ nthe");
Break
}
}
return 0;
}
void Play_choose (void)
{
System ("CLS");
printf ("\n\n\n\t\t.......................................\n\n\n");
printf ("\t\t 1-Build linked List 2-traverse list \ n");
printf ("\t\t 3-Insert element 4-delete element \ n");
printf ("\t\t 5-Sort Element 6-Clear screen (menu only) \ n");
printf ("\t\t 7-Link Table length 8-exit \ n \ nthe");
printf ("\n\n\t\t ... \n\n\n"), and so on.------------------".
}
void Create_list (Pnode phead)
{
int len;//The number of valid nodes to store
int i;
int val;//The value of the node used to temporarily store user input
Pnode pnew;
Pnode Ptail=phead;//ptail always points to the tail node, the list is empty when the ptial points to the tail nodes and phead all point to the head node.
printf ("Please enter the number of nodes you need to generate a list of: len=");
scanf ("%d", &len);

for (I=0;i<len;++i)
{
printf ("Please enter the value of%d", i+1);
scanf ("%d", &val);


Pnew = (pnode) malloc (sizeof (NODE));
if (null==pnew)
{
printf ("Allocation failed, program terminated \ n");
Exit (-1);
}
pnew->date=val;
phead->pnext=pnew;
pnew->pnext=null;
pnew->data=val;
ptail->pnext=pnew;
pnew->pnext=null;
Ptail=pnew;
}
ptail->pnext=null;
Return
}
void Traverse_list (Pnode phead)
{
if (Is_empty (phead) ==true)
{
printf ("The list is empty \ n");
Return
}
printf ("\t\t\t\t");
Pnode p=phead->pnext;
while (P!=null)
{
printf ("%d", p->data);
p=p->pnext;
}
printf ("\ n");
Return
}
BOOL Is_empty (Pnode phead)
{
if (phead->pnext==null)
{
return true;
}
Else
{
return false;
}
}
void Length_list (Pnode phead)
{
int len=0;
Pnode p=phead->pnext;
while (P!=null)
{

len++;
p=p->pnext;
}
printf ("List length is%d\n", Len);
Return
}
void Insert_list (Pnode phead)
{
Insert a new node in front of the POS node of the linked list that Phead points to, and the value of the node is Val
int i=0;
int POS;
int Val;
if (Is_empty (phead) ==true)
{
printf ("The list is empty, insert failed \ n");
Return
}
Pnode p = phead;
printf ("Please input you want to insert before the first few nodes: \ n");
scanf ("%d", &pos);
printf ("Please enter the value you want to insert: \ n");
scanf ("%d", &val);
while (P! = NULL && i < pos-1)
{
p = p->pnext;
i++;
}
if (I > pos-1| | p = = NULL)//i > pos-1 is not dispensable?????????????????????????
{
printf ("Invalid input value, insert failed");
Return
}


Pnode pnew = (pnode) malloc (sizeof (NODE));
if (pnew==null)
{
printf ("Dynamically allocating memory failed!") ");
Exit (-1);
}
Pnew->data = val;
Pnode q = p->pnext;
P->pnext = pnew;
Pnew->pnext = q;
printf ("Insert succeeded \ n");
Return
}
void Delete_list (Pnode phead)//Delete node
{
int i=0;
int POS;
if (Is_empty (phead) ==true)
{
printf ("The list is empty, delete failed \ n");
Return
}
Pnode p = phead;
printf ("Please enter the first few nodes you want to delete: \ n");
scanf ("%d", &pos);


while (p->pnext! = NULL && i < pos-1)
{
p = p->pnext;
i++;
}
if (I > pos-1| | P->pnext = = NULL)//i > pos-1 is not dispensable?????????????????????????
{
printf ("Invalid input value, delete failed \ n");
Return
}


Pnode q=p->pnext;
printf ("The element you deleted is:%d\n", q->data);
p->pnext=p->pnext->pnext;
Free (q);
Q=null;
printf ("Delete succeeded \ n");
Return
}
void Sort_list (Pnode phead)
{
if (Is_empty (phead) ==true)
{
printf ("The list is empty, sort failed \ n");
Return
}
int t;
int i,j,t;
int len=length_list (phead);
Pnode p,q;
The following is written against the array
for (P=phead->pnext;p->pnext!=null;p=p->pnext)//similar to array: for (i=1;i<len-1;i++)
{
for (Q=p->pnext;q!=null;q=q->pnext)//similar to array: for (j=1+i;j<len;j++)
{
if (p->data>q->data)//similar to array: if (A[i]>a[j])
{
t=p->data; Similar to the array: T=a[i];
p->data=q->data; Similar to the array: A[i]=a[j];
q->data=t; Similar to the array: a[j]=t;
}
}
}
printf ("Sort succeeded \ n");
Return
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure linked list operation set (build, Traverse, insert, delete, sort, length, empty judgment, etc.)

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.