Learning notes-basic operations for single-chain table using C language: Create, output, insert, delete, and reverse linked list

Source: Internet
Author: User

The linked list is the simplest data structure that every software developer must master and is also the most common content for enterprise recruitment. Therefore, here we will summarize some basic operations on a single-chain table.

Note: Here is a one-way, non-circular linked list, where data is stored in the header node. In other words, there is no difference between the header node and other nodes. You can also say that the linked list does not take the lead node. The source code of the linked list operation is as follows:

# Include <stdio. h>
# Include <stdlib. h>

Typedef struct Node
{
Int data;
Struct node * next;
} Lnode;

Void printlist (lnode * head );
Lnode * createlist ();
Lnode * insertlist (lnode * head, int ELEM, int position );
Lnode * deleteelem (lnode * head, int ELEM );
Lnode * reverselist (lnode * head );

 

Int main ()
{
Lnode * head;
Int ELEM, position;

Head = createlist ();
Printlist (head );

Printf ("input the element and the position:/N ");
Scanf ("% d", & ELEM, & position );
Head = insertlist (Head, ELEM, position );
Printlist (head );

Head = reverselist (head );
Printf ("after reversed:/N ");
Printlist (head );

Printf ("input the element you want to delete:/N ");
Scanf ("% d", & ELEM );
Head = deleteelem (Head, ELEM );
Printlist (head );

Return 0;
}

 

/* Input the header pointer and print the linked list */
Void printlist (lnode * head)
{
Lnode * P = head;

If (P = NULL)
{
Printf ("list is empty! /N ");
Return;
}
While (P! = NULL)
{
Printf ("% d", p-> data );
P = p-> next;
}
Printf ("/N ");
}

 

/* Create a New linked list and return the header pointer */
Lnode * createlist ()
{
Lnode * head, * Current, * P;
Int data;

Head = (lnode *) malloc (sizeof (lnode ));
P = head;

Printf ("input integers, 0 to break:/N ");
Scanf ("% d", & data );

If (Data! =-1)
{
P-> DATA = data;
}
Else
{
Head-> next = NULL; // only the header Node
Return head;
}

While (1)
{
Scanf ("% d", & data );
If (Data =-1)
{
Break;
}

Current = (lnode *) malloc (sizeof (lnode ));
Current-> DATA = data;
P-> next = current;
P = p-> next;
}
P-> next = NULL;

Return head;
}

 

/* Insert a linked list. The head is the table header, the ELEM is the element to be inserted, and the position is the inserted position.
* Insert ELEM to the position node. When position <= 0, insert it to the header node.
*/
Lnode * insertlist (lnode * head, int ELEM, int position)
{
Lnode * Current, * P;
Int I = position-1;
Current = head;

/* Locate the position node */
While (I> 0 & Current-> next! = NULL)
{
Current = Current-> next;
I --;
}

If (I> 0) // The number of knots is smaller than the input position
{
Printf ("position is wrong! /N ");
Return head;
}

P = (lnode *) malloc (sizeof (lnode ));
P-> DATA = ELEM;
P-> next = Current-> next;
Current-> next = P;

Return head;
}

 

/* Delete all the elements ELEM in the head of the linked list and return the header node head */
Lnode * deleteelem (lnode * head, int ELEM)
{
Lnode * P = head;
Lnode * ptemp;
Int flag = 0; // record the number of ELEM instances in the table

While (1)
{
If (Head-> DATA = ELEM) // Delete the header Node
{
Flag ++;
Head = head-> next;
Free (P );
P = head;
}
Else
{
Break;
}
}

While (p-> next! = NULL)
{
If (p-> next-> DATA = ELEM)
{
Flag ++;
Ptemp = p-> next;
If (p-> next! = NULL)
{
P-> next = p-> next;
Free (ptemp );
Ptemp = NULL;
}
Else // last Node
{
Free (ptemp );
Ptemp = NULL;
P-> next = NULL;
Break;
}
}
Else // query the next node if the node is not deleted
{
P = p-> next;
}
}
Printf ("% d Number '% d' was found and deleted./N", flag, ELEM );
Return head;
}

 

/* Sort the linked list in reverse order. The linked list does not take the lead node. */
Lnode * reverselist (lnode * head)
{
Lnode * Current, * pnext, * ptemp;

Current = head;
Pnext = head-> next;
Head-> next = NULL;

While (pnext! = NULL)
{
Ptemp = pnext-> next;
Pnext-> next = current;
Current = pnext;
Pnext = ptemp;
}

Head = current;
Return head;
}

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.