#include <iostream.h>
struct student
{
int num;
int score;
struct student *next;
};
int n;
struct student *CreateLink()
{
struct student *head;
struct student *p1=NULL;
struct student *p2=NULL;
int num = 1;
int score = 11;
n = 0;
p1 = new struct student();
p2 = p1;
if (p1 == NULL)
{
cout<<"/nCann't create it, try it again in a moment!/n";
return NULL;
}
else
{
head = NULL;
p1->num = num++;
p1->score = score++;
}
while(p1->num < 10)
{
n += 1;
if (n==1)
{
head = p1;
p2->next = NULL;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = new struct student();
p1->num = num++;
p1->score = score++;
}
p2->next = NULL;
delete(p1);
p1 = NULL;
return head;
}
void Print(struct student *head)
{
struct student *p;
p = head;
if(head != NULL)
{
do
{
cout<
<<'/T'<
num<<'/t'<
score<<'/t'<
next<
p = p->next;
}
while (p != NULL);
}
}
struct student *DeleteLink(struct student *head, int num)
{
struct student *p1;
struct student *p2;
if (head == NULL)
{
cout<<"/nList is null!/n";
return head;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
delete p1;
p1 = NULL;
n -= 1;
}
else
{
cout<<"/nnot been found!/n";
}
return head;
}
struct student *InsertLink(struct student *head, int num, struct student *node)
{
struct student *p1;
if (head == NULL)
{
head = node;
node->next = NULL;
n += 1;
return head;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p1 = p1->next;
}
if (num == p1->num)
{
node->next = p1->next;
p1->next = node;
n += 1;
}
else
{
cout<<"/nnot been found!/n";
}
return head;
}
struct student *ReverseLink(struct student *head)
{
struct student *p;
struct student *p1;
struct student *p2;
p1 = NULL;
p2 = head;
while (p2 != NULL)
{
p = p2->next;
p2->next = p1;
p1 = p2;
p2 = p;
}
head = p1;
return head;
}
void main()
{
struct student *head;
struct student *stu;
int thenumber;
head = CreateLink();
Print(head);
cout<<"/nWhich one delete: ";
cin>>thenumber;
head = DeleteLink(head,thenumber);
Print(head);
stu = new struct student();
cout<<"/nPlease input insert node -- num,score: ";
cin>>stu->num>>stu->score;
cout<<"/nInsert behind num: ";
cin>>thenumber;
head = InsertLink(head,thenumber,stu);
Print(head);
head = ReverseLink(head);
Print(head);
}