只为复习链表。。。。。。。。

定义一个结构体链表存储某班级学生的学号、姓名和三门课程的成绩,实现增加、修改、删除和查询记录的功能。(每次只有一次操作)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
char s[10];
struct Student
{
    int id;
    char name[109];
    int a[4];
    struct Student *next_point;//next 连接地址
};
Student *head,*p1,*p2;
int n;
void create()//创建链表
{
    scanf("%d",&n);
    p1=p2=new Student;
    for(int i=1;i<=n;i++)
    {
        scanf("%d %s",&p1->id,p1->name);
        for(int j=0;j<3;j++)
            scanf("%d",&p1->a[j]);
        if(i==1) head=p1;
        else p2->next_point=p1;
        p2=p1;
        p1=new Student;
    }
    p2->next_point=NULL;
}
void add()//增加节点
{
    struct Student *p=new Student;
    int t;
    scanf("%d",&t);
    p1=head;
    scanf("%d %s",&p->id,p->name);
    for(int i=0;i<3;i++)
        scanf("%d",&p->a[i]);
    if(t==1)
    {
        head=p;
        if(n) head->next_point=p1;
        else  head->next_point=NULL;
    }
    else
    {
        t-=2;
        while(p1!=NULL && t--) p1=p1->next_point;
        if(p1->next_point==NULL) {p1->next_point=p;p->next_point=NULL;}
        else {
            p->next_point=p1->next_point;//注意,避免陷入死循环
            p1->next_point=p;
        }
    }
}
void delet()//删除节点
{
    int t;
    p1=head;
    if(head==NULL) return ;
    scanf("%d",&t);
    if(t==1)
    {
        if(p1->next_point==NULL) head=NULL;
        else head=p1->next_point;
    }
    else
    {
        t-=2;
        while(p1!=NULL && t--) p1=p1->next_point;
        if(p1->next_point->next_point==NULL) p1->next_point=NULL;
        else p1->next_point=p1->next_point->next_point;
    }
}
void uppdate()//更新某一节点
{
    p1=head;
    Student *p=new Student;
    int t;
    scanf("%d",&t);
    scanf("%d %s",&p->id,p->name);
    for(int i=0;i<3;i++)
        scanf("%d",&p->a[i]);
    if(t==1)
    {
        if(p1->next_point==NULL) head=p,head->next_point=NULL;
        else p->next_point=p1->next_point,head=p;
    }
    else
    {
        t-=2;
        while(p1!=NULL && t--) p1=p1->next_point;
        if(p1->next_point->next_point==NULL) p1->next_point=p,p->next_point=NULL;
        else p->next_point=p1->next_point->next_point,p1->next_point=p;
    }
}
void print_Student()
{
    Student *p=head;
    if(head!=NULL)
    {
        do
        {
            printf("%d %s",p->id,p->name);
            for(int i=0;i<3;i++)
                printf(" %d",p->a[i]);
            printf("
");
            p=p->next_point;
        }while(p!=NULL);
    }
}
int main()
{
    create();
    scanf("%s",s);
    if(s[0]=='a') add();
    else if(s[0]=='u') uppdate();
    else delet();
    print_Student();
    return 0;
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7647618.html