Linear linked list of data structures

Source: Internet
Author: User

#include <stdio.h>
#include <malloc.h>
/*
Declares the struct type of a struct node,
Contains the data field and the pointer field to the struct node variable.
using typedef, node instead of struct node, pnode instead of struct node*
*/
typedef struct NODE
{
int data;
struct node* next;
}node,*pnode;

Declaration of a linked list operation function
Pnode create_list ();
int length_list (pnode);
void Sort_list (Pnode);
BOOL Insert_list (pnode,int,int);
BOOL Delete_list (pnode,int,int*);
void Traverse_list (Pnode);

int main ()
{
int len=0;//defines the variable len, storing the length of the linked list
int val=0;//The value where the linked list is deleted
Pnode Phead=null;
Phead=create_list ();//phead points to the head node of the linked list
Len=length_list (Phead);
printf ("The length of the list:%d\n", Len);

printf ("original linked list: \ n");
Traverse_list (Phead);//Traverse list, output

Sort_list (Phead);//Sort the list
printf ("sorted list: \ n");
Traverse_list (Phead);

if (Insert_list (phead,2,10))//Insert 44 in the second position
{
printf ("linked list after inserting elements: \ n");
Traverse_list (Phead);
}

if (Delete_list (phead,2,&val))//delete the element at position 2
{
printf ("Deleted value:%d\n", Val);//output Delete the value of the element
Traverse_list (Phead);
}
return 0;
}

/*
Creates a linked list of the specified size and assigns a value to its data field,
Returns the address of the head node
*/
Pnode create_list ()
{
int Len,val;//len is the chain table length, Val holds the value of the input data field
printf ("Please enter the number of elements:");
scanf ("%d", &len);

Pnode phead= (pnode) malloc (sizeof);//apply for a dynamic space of node, Phead point to that memory
Pnode Ptail=phead;//ptail points to the last element of the list, assigning the address of the head node to Ptail
ptail->next=null;//set the pointer field of the head node to null
Pnode Pnew=null;
for (int i=0;i<len;i++)//for loop add linked list element
{
printf ("Please enter the value of the element:");
scanf ("%d", &val);
pnew= (pnode) malloc (sizeof);//apply for a dynamic space of Node, pnew points to that memory
pnew->data=val;//assigns Val to Pnew's data field
ptail->next=pnew;//assigns pnew to Ptail's pointer field and adds it to the end of the list.
pnew->next=null;//set the pointer field of the pnew to null
ptail=pnew;//again ptail to the last element of the list
}
Return phead;//address of the head node
}

/*
Address of the incoming header node, using while loop to traverse the output list.

*/
void Traverse_list (Pnode phead)
{
Pnode p=phead->next;//assigns the pointer field of the head node to p.
while (P!=null)//Determine if P is empty, if NULL then jump out of the loop
{
printf ("%d\t", p->data);//data field for output p
P=p->next;//p refers to the downward one pointer field
}
printf ("\ n");
Return
}

/*
Address of incoming head node, using while loop to return the length of the linked list

*/
int length_list (Pnode phead)
{
Pnode p=phead->next;
int len=0;
while (P!=null)
{
len++;
p=p->next;
}
Return len;//returns the chain table length len
}

/*
The address of the incoming header node, the insertion position POS and the inserted value Val,
First locate the address of the previous element of the inserted POS position, and then assign the Pnew pointer field to the Pos-1 pointer field.
Point the Pos-1 's pointer field to Pnew

*/
BOOL Insert_list (Pnode phead,int pos,int val)
{
int i=0;
Pnode P=phead;
while (i<pos-1&&p!=null)//Use while loop to find the previous element of POS
{
P=p->next;//p pointing to the next element
i++;//position Gaga
}
if (i>pos-1| | P==null)//Incorrect position inserted, return false
{
printf ("The inserted location is unreasonable!") \ n ");
return false;
}
Pnode pnew= (pnode) malloc (sizeof (Node));
pnew->data=val;
pnew->next=p->next;//assigns the pointer field of p to the pointer field of Pnew
p->next=pnew;//points the pointer field of P to pnew
return true;
}

/*
Address of the incoming header node, insert location POS and the address of the variable Val,
Locate the address of the pos-1 location first, and then assign the data to the pointer field of the element to *val
Assigns the Pos-1 pointer field to the next element's pointer field

*/
BOOL Delete_list (Pnode phead,int pos,int* val)
{
int i=0;
Pnode P=phead;
while (i<pos-1&&p!=null)//Find the element at POS location
{
p=p->next;
i++;
}
if (i>pos-1| | P==null)
{
printf ("The location of the deletion is unreasonable!") \ n ");
return false;
}
Pnode q=p->next;//Pointer q points to the element that will be deleted
*val=q->data;//assigns the data that Q points to *val
p->next=p->next->next;//the pointer field of the previous element of the deleted element to a pointer field that deletes the element
Free (q);//Release Q-Pointing memory
return true;
}

/*
The address of the incoming head node, using the selection sorting algorithm to sort the list.

*/
void Sort_list (Pnode phead)
{
Pnode p,q;//defining the structure pointer p,q
int len=length_list (phead);//Get the length of the list Len
int i,j,temp;
for (I=0,p=phead->next;i<len-1;i++,p=p->next)//initial variable p first node address, increment p points to pointer field
{
for (J=i+1,q=p->next;j<len;j++,q=q->next)
{
if (p->data>q->data)//Ascending sort
{
temp=p->data;//uses temp to swap the value of P->data with Q->data
p->data=q->data;
q->data=temp;
}
}
}
}

Linear linked list of data structures

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.