- 作者:xiaoxiao
- 发表时间:2020-12-23 10:57
- 来源:未知
#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"; }