// Insert and delete a file linked list. cpp
# Include "iostream. H"
# Include "string. H"
# Include "stdlib. H"
Struct listnode {// type declaration of the linked list Node
Int num;
Char name [10];
Char sex [4];
Int age;
Int score;
Listnode * next;
};
Listnode * head; // head points to the header Node
Listnode * createlist () // create a linked list with an empty Header
{
Listnode * temp, * tail; // tail points to the End Node of the table in the current linked list
Head = new listnode; // create an empty Header
If (Head = NULL ){
Cout <"memory allocate fail and exit ";
Exit (1 );
}
Tail = head;
Tail-> next = NULL;
While (1 ){
Temp = new listnode;
If (temp = NULL ){
Cout <"memory allocate fail and exit ";
Exit (1 );
}
Cout <"Enter student information (Num, name, sex, age and score) (when num is-1 to stop !) /N ";
Cin> temp-> num;
If (temp-> num =-1) break;
Else
Cin> temp-> Name> temp-> sex> temp-> age> temp-> score;
Tail-> next = temp; // Insert the new node temp after the end of the current table
Tail = temp;
Tail-> next = NULL;
};
Delete temp;
Return head;
}
Void browser (listnode * listhead) // "traverses all nodes of the" linked list"
{
Cout <"/n student information:/N ";
Listnode * temp = listhead-> next;
While (temp! = NULL ){
Cout <temp-> num <""
<Temp-> name <""
<Temp-> sex <""
<Temp-> age <""
<Temp-> score <Endl;
Temp = temp-> next;
}
}
Int lenoflist (listnode * head) // calculates the number of nodes in the head of the linked list.
{
Listnode * temp;
Temp = head-> next;
Int lenofnode = 0;
While (temp! = NULL ){
Lenofnode ++;
Temp = temp-> next;
}
Return lenofnode;
}
Listnode * insert (listnode * head, int num)
// Insert a new node after the num node of the head of the linked list
{
// Judge the correctness of num
If (Num <0 | num> lenoflist (head) {// when num = 0, insert new node as the first node
Cout <"parameter num error! "<Endl;
Return head;
}
Listnode * position = head;
Int counter = 0;
For (int K = 0; k <num; k ++) // calculates the position of the num Node
Position = position-> next;
// Insert a new node after the location temp
Cout <"Enter the student information (Num, name, sex, age and score):/N ";
Listnode * temp; // temp is used to save the student information to be inserted.
Temp = new listnode;
Cin> temp-> num> temp-> Name> temp-> sex> temp-> age> temp-> score;
// Insert this node after position
Temp-> next = position-> next;
Position-> next = temp;
Return head;
}
Listnode * deletenode (listnode * head, int num)
// Delete the num node in the head of the linked list
{
// Judge the correctness of num
If (Num <1 | num> lenoflist (head )){
Cout <"parameter num error! "<Endl;
Return head;
}
Listnode * position = head;
For (int K = 1; k <num; k ++) // calculates the position of the (num-1) node
Position = position-> next;
// Delete the num Node
Listnode * temp; // temp is used to save the node to be deleted.
Temp = position-> next;
Position-> next = temp-> next;
Delete temp;
Return head;
}
Void main (void)
{
Head = createlist ();
Cout <"the length of the linked list is" <lenoflist (head) <Endl;
Browser (head); // you can use browser (createlist ());
Cout <"after the input is inserted to the students? ";
Int num;
Cin> num;
Browser (insert (Head, num ));
Cout <"Enter the number of students to delete? ";
Cin> num;
Browser (deletenode (Head, num ));
}