#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof (struct Student)
struct Student
{
Long num;
Float score;
struct Student*next;
};
int n;
int main ()
{
/*-----------------------------Program Description--------------------------------------------
Title: Write out a main function, call to create a list of functions create (), Output linked list function print (),
Deletes the function del () of the linked list node, inserts the function insert (), altogether 5 functions.
Author:killerlegend
date:2013.12.6
----------------------------------------------------------------------------------*/
function declaration
struct student* Create ()//creation of a dynamic list function declaration, function type is Student struct type, return header pointer
struct student* del (struct student*, long)//delete function declaration for specified location node, parameter: Link header node + delete node position + RETURN header pointer
struct student* Insert (struct student*,struct student*);//function declaration for inserting a Student type of data
void print (struct student*)//function declaration of data in the output list, parameter is the head pointer of the linked list
Defining variables
struct Student *head,*stu;//defines the head pointer and new node of a dynamic list
Long Del_num;
Establish a linked list operation
printf ("Input records:\n");
Head = Create ();//create a linked list and return the header pointer
Print (head);//Output all nodes
Delete a node operation
printf ("\ninput the deleted Number:");
scanf ("%ld", &del_num);
while (del_num!=0)//When input number is 0 o'clock end loop
{
Head = del (head,del_num);//Return the header address of the list after the node is deleted
Print (head);//Output all nodes
printf ("Input the deleted Number:");
scanf ("%ld", &del_num);
}
Insert Node action
printf ("\ninput The inserted number:");
stu= (struct student*) malloc (LEN);//each insertion of a node requires a new node to be opened
scanf ("%ld%f", &stu->num,&stu->score);
while (stu->num!=0)//When the input school number is 0 o'clock to end the loop
{
Head = insert (HEAD,STU);//Return header address of list
Print (head);
printf ("\ninput The inserted number:");
Stu = (struct student*) malloc (LEN);
scanf ("%ld%f", &stu->num,&stu->score);
}
return 0;
}
Create a function of a linked list
struct student* Create ()
{
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2= (struct Student *) malloc (LEN);
scanf ("%ld%f", &p1->num,&p1->score);
Head=null;
while (p1->num!=0)
{
n++;
if (n==1) head=p1;
Else p2->next=p1;//The first time it is executed, this step is to point the head pointer to itself, and when N starts at 2, this step is used to point the P2 to the next element
p2=p1;//make P2 and P1 point to the same storage area
p1= (struct student*) malloc (LEN);//Open dynamic Store, force return pointer of struct Student type
scanf ("%ld%f", &p1->num,&p1->score);
}
p2->next=null;
return (head);
}
function to delete a node
struct student* del (struct student* head,long num)
{
struct Student *p1,*p2;
if (head==null)
{
printf ("List null!\n");
return (head);
}
P1=head;
while (Num!=p1->num && p1->next!=null)
{
P2=P1;
p1=p1->next;
}
if (num==p1->num)
{
if (P1==head)
{
head=p1->next;
}
Else
{
p2->next=p1->next;
}
printf ("delete:%ld\n", num);
n=n-1;
}
Else
{
printf ("%ld not been found!", num);
}
return (head);
}
function to insert a node
struct student* Insert (struct student* head,struct Student * stud)
{
struct Student *p0,*p1,*p2;
P1=head;
P0=stud;
if (head==null)//The original linked list is empty table
{
head=p0;p0->next=null;//the inserted node as the head node when empty table
}
else//if it is not an empty table, traverse to find the appropriate insertion position
{
while ((P0->num>p1->num) && (p1->next!=null))//inserted in the order of number, if the number of inserted school numbers is larger, you should go backwards
{
P2=P1;
p1=p1->next;//Move back
}
}
if (p0->num<=p1->num)//Find the insertion position, the insertion position is before the position P1 points to, that is, the P2 point to the location
{
if (HEAD==P1) head=p0;//causes the head to point to p0 if the insertion position is before the header position
Else p2->next=p0;//The P2 next pointer to the inserted data address if it is not before the header position P0
p0->next=p1;//the P0 next pointer to the P1 and completes the data entry.
}
else//inserted the school number position in the last one
{
p1->next=p0;
p0->next=null;
}
n=n+1;//record number plus one
return (head);
}
function of output linked list
void print (struct Student * head)
{
struct Student * p;
printf ("Now,these%d records are:\n", N);
P=head;
if (head!=null)
Todo
{
printf ("%ld%5.1f\n", P->num,p->score);
p=p->next;
}while (P!=null);
}