⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7-2-1.cpp

📁 学习c++的ppt
💻 CPP
字号:
#include <iostream.h>
struct student  {
      int        num;
      float     score;
      struct student   *next;
};   // 学生节点定义
int n = 0;
struct student *create(void)
{    struct student *head, *p1, *p2;
      p1 = p2 = new  struct student; 
      cin >> p1->num >> p1->score;
      head = NULL;
      while(p1->num != 0)  {
           n ++;
           if(n == 1)  head = p1;
           else p2->next = p1;
           p2 = p1;
         p1 = new  struct student;
         cin >> p1->num >> p1->score;
     }
     p2->next = NULL;
     return(head);
}
void print(struct student *head)
{  struct student *p = head;
   cout << "链表数据如下:" << endl;
   if(head != NULL)
        do  {
             cout << p->num << ", " << p->score << endl;
             p = p->next;
        } while(p!=NULL);
 }
struct student *deletelist(struct student *head, int num)
{   struct student *p1, *p2;
     if(head == NULL)  {
      cout << "This list is NULL" << endl;
      return(NULL);
  }
  p1 = head;
  while(p1->num != num && p1->next != NULL){
      p2 = p1;
      p1 = p1->next;
  }
  if(p1->num == num)  {
        if(p1 == head) head = p1->next;  // 删除首结点    
        else p2->next = p1->next;    // 删除中间或尾结点  
        cout << "delete:" << num << endl;
        delete p1;
  } else  cout << num << " not been found !" << endl;
  return(head);
}
struct student *insert(struct student *head, struct student *stud)
{   struct student *p0, *p1, *p2;
     p1 = head; p0 = stud;
     if(head == NULL)  {
       head = p0;  p0->next = NULL; return(head);
 }
 while((p0->num > p1->num) && 
            (p1->next != NULL))  {
p2 = p1;  p1 = p1->next;
}
if(p0->num <= p1->num)  {  
     if(head == p1)  head = p0;  //  插在头部  //
     else p2->next = p0;  // 插在 p2和p1之间  //
     p0->next = p1;
} else {  //  插到最后  //
     p1->next = p0;  p0->next = NULL; }
     return(head); 
}

main()
{     struct student  *head, *p0;
      head = create();
      print(head);
	  deletelist(head, 1006);
	  print(head);
	  p0 = new struct student;
	  cin >> p0->num >> p0->score;
	  insert(head, p0);
	  print(head);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -