Create, add, and delete a one-way linked list

Source: Internet
Author: User



/* ------------------------- Contains the header file ------------------------------------*/
# Include
# Include
# Include
# Include
Int count = 0;
/* ------------------------- Structure definition section ------------------------------*/
Typedef struct Node
{
Char name [10];
Int score;
Struct Node * next;
} ListNode;


/* ---------------------------- Function declaration section ------------------------------*/




/* --------------------------- Function implementation section -------------------------------*/
/* ----------------------------- Create a linked list ---------------------------------*/
/* Insert a new node at the end of the linked list to create a linked list */
ListNode * CreateList ()
{
ListNode * head; // pointer to the header Node
ListNode * p, * pre;
Head = (ListNode *) malloc (sizeof (ListNode); // allocate memory space for the header Node
Head-> next = NULL; // clear the pointer field of the header Node
Pre = head; // first assign the first address of the header node to the intermediate variable pre
While (1)
{
If (p = (ListNode *) malloc (sizeof (ListNode) = NULL)
{
Printf ("insufficient memory !!! \ N ");
Break;
}
Count ++;
Printf ("input name of the % d student (input \" q \ "to quit):", count); // print the name of the person (s)
// Memory space p points to the first address of the newly inserted Node
Scanf ("% s", & p-> name); // enter the name
If (strcmp (p-> name, "q") = 0)
Break;
Printf ("input score of the % d student:", count );
Scanf ("% d", & p-> score); // enter the score
Pre-> next = p; // point p to the new node and insert it to the linked list, that is, the header node pointer field points
// Next node
// The first node points to p, because the content of the header node is empty.
Pre = p; // This is used to point to the next node.
Pre-> next = NULL;
}
Return head; // return the first address of the linked list.
}
/* ------------------------- Output linked list -----------------------------------*/
Void PrintList (ListNode * h)
{
ListNode * p;
P = h-> next;
While (p)
{
Printf ("% s, % d", p-> name, p-> score );
P = p-> next;
Printf ("\ n ");
}
}
/* ---------------------- Insert the linked list node --------------------------*/
/*--------------------------------------------------------------------
Function name: InsertList (ListNode * h, int I, char name [], int e, int n)
Function: Insert a linked list Node
Entry parameter: h: Header node address I: insert to the nth node name: insert node name e: insert node score n: number of nodes in the linked list except the number of the next node
Exit parameters:
--------------------------------------------------------------------*/
Void InsertList (ListNode * h, int I, char name [], int e, int n)
{
ListNode * p, * q; // first define two pointers pointing to a node
If (I <1 | I> n + 1)
Printf ("Error !!! \ N ");
Else
{
Int j = 1;
P = h; // point the pointer p to the header node of the linked list
While (j {
P = p-> next;
J ++;
}
If (q = (ListNode *) malloc (sizeof (ListNode) = NULL)/* is
Memory space allocated by nodes */
Printf ("insufficient memory !!! \ N ");
Else
{
Count ++;
Strcpy (q-> name, name); // copy the name to the node to be inserted
Q-> score = e; // score assignment for the node to be inserted
Q-> next = p-> next;/* This is to direct the newly inserted node pointer field
The Node Address pointed to by the previous node pointer field is p-> next */
P-> next = q;/* the node pointer field before the node location to be inserted
Point to the first address of the inserted node */
}
}
}


/*--------------------------------------------------------------------
Function Name: DeleteList (ListNode * h, int I, int n)
Function: delete a linked list node.
Entry parameter: h: Header node address I: Location of the node to be deleted n: number of nodes in the linked list, except the number of lower-end nodes
Exit parameters:
--------------------------------------------------------------------*/
Void DeleteList (ListNode * h, int I, int n)
{
ListNode * p, * q; // first defines two pointers pointing to the node-type struct.
Char name [10];
Int score;
If (I <1 | I> count + 1)
Printf ("Error !!! \ N ");
Else
{
Int j = 1;
P = h; // point the pointer to the first address of the head node of the linked list
While (j {
P = p-> next;
J ++;
}
Q = p-> next;/* q points to the node pointer field pointing to the location before the location to be deleted
The node pointed to by address q is the node to be deleted */
P-> next = q-> next;/* this is the first node of the node to be deleted.
Pointer field pointing to the next node in the pointer field of the node to be deleted
The first address is used to delete the I-th node */
Strcpy (name, q-> name );
Score = q-> score;
Free (q); // release the node to which q points
Printf ("name: % s \ tscore: % d has been deleted !!! \ N ", name, score );
}
}


/* -------------------------- Main function -------------------------------*/
Int main ()
{
ListNode * h; // h points to the struct NODE
Int I = 1, n, score;
Char name [10];


While (I)
{
/* Enter the prompt information */
Printf ("1 -- create a new linked list \ n ");
Printf ("2 -- add element \ n ");
Printf ("3 -- delete element \ n ");
Printf ("4 -- output the element \ n in the current table ");
Printf ("0 -- exit \ n ");


Scanf ("% d", & I );
Switch (I)
{
Case 1:
H = CreateList ();/* Create a linked list */
Printf ("list elements is: \ n ");
PrintList (h );
Break;


Case 2:
Printf ("input the position. of insert element :");
Scanf ("% d", & I );
Printf ("input name of the student :");
Scanf ("% s", name );
Printf ("input score of the student :");
Scanf ("% d", & score );
InsertList (h, I, name, score, count );
Printf ("list elements is: \ n ");
PrintList (h );
Break;


Case 3:
Printf ("input the position of delete element :");
Scanf ("% d", & I );
DeleteList (h, I, count );
Printf ("list elements in: \ n ");
PrintList (h );
Break;


Case 4:
Printf ("list element is: \ n ");
PrintList (h );
Break;
Case 0:
Return;
Break;
Default:
Printf ("ERROR! Try again! \ N ");
}
}
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.