Create, insert, delete, and invert a single-chain table

Source: Internet
Author: User
Tags define null

/*-----------------------------------------------------*/
/* Create, insert, delete, and invert a single-chain table -----------*/
/* ------------ Written by redfire250 ----- 2005.5.10 ----*/
/*-----------------------------------------------------*/
# Include <malloc. h>
# Include <stdio. h>
# Define null 0
Struct student
{
Long number;
Char name [20];
Long score;
Struct student * next;
};
Int n = 0;/* n is the global variable used to calculate the number of nodes in the linked list */
/*-----------------------------------------*/
/* -------------- Create the node function creat ()--------*/
/*-----------------------------------------*/
Struct student * creat ()
{
Struct student * P1;
Struct student * P2;
Struct student * head = NULL;
P1 = P2 = (struct student *) malloc (sizeof (struct student);/* open up an available memory unit */
Printf ("Please input the student's number name and the score: \ n ");
Scanf ("% LD % S % lD", & p2-> Number, P2-> name, & p2-> score );
While (P2-> number! = 0)
{
N ++;
If (n = 1)/* indicates whether the first node is opened */
Head = P2;
Else
P1-> next = P2;
P1 = P2;
P2 = (struct student *) malloc (sizeof (struct student ));
Printf ("input the number the name and the score: \ n ");
Scanf ("% LD % S % lD", & p2-> Number, P2-> name, & p2-> score );
}
P1-> next = NULL;
Return (head );
}
/*------------------------------------------*/
/* -------------- View the linked list content function view ()------*/
/*------------------------------------------*/
View (struct student * head)
{
Struct student * P;
P = head;
While (p-> next! = NULL)
{
Printf ("% LD % S % LD \ n", p-> Number, p-> name, p-> score );
P = p-> next;
}
Printf ("% LD % S % LD \ n", p-> Number, p-> name, p-> score );
}
/*-------------------------------------------------*/
/* -------------- Insert node function (inserted before) insert ()-------*/
/*-------------------------------------------------*/
Insert (struct student * head, int num)/* head is the head pointer of the linked list, And num is inserted into the position of the linked list */
{
Int T = 1;
Struct student * P1, * P2;
P1 = head;
If (Num> N | num <0)
{
Printf ("input error !!! \ N ");
Return 0;
}
While (T <Num-1)/* Find the previous node to insert the node */
{
P1 = p1-> next;
T ++;
}
P2 = (struct student *) malloc (sizeof (struct student ));
Printf ("input the number the name and the score: \ n ");
Scanf ("% LD % S % lD", & p2-> Number, P2-> name, & p2-> score );
P2-> next = p1-> next;
P1-> next = P2;
N ++;
}
/*------------------------------------------*/
/* ------------ Delete the node function delnode ()--------*/
/*-----------------------------------------*/
Delnode (struct student * head, int node)
{
Int T = 1;
Struct student * P1, * P2;
P2 = head;
If (node> N | node <1)
{
Printf ("error !!! The node is not exist! ");
Return 0;
}
While (T <node-1)/* Find the previous node of the node to be deleted */
{
P2 = P2-> next;
T ++;
}
P1 = P2-> next;/* Find the last node to be deleted */
Free (P2-> next);/* release the Node space to be deleted (delete )*/
P2-> next = p1;/* point the previous node to the next node */
N --;
}
/*-------------------------------------------------*/
/* -------------- Reverse reorganization of the linked list invert ()-------*/
/*-------------------------------------------------*/
Struct student * invert (struct student * head)
{
Struct student * P1, * P2;
P1 = head;
P2 = p1-> next;
Head = P2-> next;
P1-> next = NULL;
While (Head-> next! = NULL)
{
P2-> next = p1;
P1 = P2;
P2 = head;
Head = head-> next;
}
Head-> next = P2;
P2-> next = p1;
Return head;
}
Main ()
{
Int number1, number2;
Struct student * head;
Head = creat ();
View (head );
Printf ("the n that you want to insert: \ n ");
Scanf ("% d", & number1 );
Insert (Head, number1 );
View (head );
Printf ("the node that you want to delete: \ n ");
Scanf ("% d", & number2 );
Delnode (Head, number2 );
View (head );
Printf ("Inverte the list: \ n ");
View (invert (head ));
Getch ();
}

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.