2017/3/30课堂小练习-------XXX系统(链表插入删除打印)

懒得打printf和scanf,就用了C++

改成C版本的只需要

            1.把cin >> xxx>>cout改成scanf(“%*”,&xxx)

            2.把cout << xxx <<endl 改成printf(“%*”,XXX);

            3.把#include<c****>的头文件改成#include<****.h>就好

             4.去掉第二行的

#include <iostream>
这个头文件

之所以用%*替代是因为有的时候输入的字符串,有的时候输入的是数字,自己依照情况改改就好

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#define maxn 500
using namespace std;
typedef struct Node
{
    char home[maxn];
    int id;
    char name[maxn];
    struct Node *next;
} Node;

int Empty(Node *first)
{
    if(first->next == NULL)
        return 1;
    return 0;
}
Node *CreatTailList()
{
    Node *first = (Node *)malloc( sizeof(Node));
    return first;
}

void PrintList(Node*first)
{
    Node*p = first -> next;
    if(Empty(first))
    {
        cout << "系统内暂无数据" << endl;
        return;
    }
    while(p != NULL)
    {
        cout <<p->id <<"\t"<<p->name<<"\t"<<p->home<<endl;
        p = p->next;
    }
}

int Locale(Node *first,int x)
{
    Node *p = first->next;
    while(p!=NULL || x!=p->id)
        p = p -> next;
    if(p==NULL)
        cout << "查无此人" << endl;
    else
        cout <<p->id <<"\t"<<p->name<<"\t"<<p->home<<endl;
    return 0;
}
void Insert(Node *first,int x,Node t)
{
    Node *s = NULL,*p = first;
    int cnt = 0;
    while ( p != NULL && cnt < x - 1)
    {
        p = p -> next;
        cnt ++;
    }
    if(p == NULL )
    {
        cout << "插入位置错误" <<endl;
        return;
    }
    s = (Node*)malloc(sizeof(Node));
    s -> id = t.id;
    strcpy(s->name,t.name);
    strcpy(s->home,t.home);
    s -> next = p -> next;
    p -> next = s;
}


void Delete(Node *first,int i)
{
    Node *p = first,*q = NULL;
    int cnt = 0;
    while(p != NULL && cnt < i - 1)
    {
        p = p -> next;
        cnt++;
    }
    if(p == NULL || p-> next == NULL)
    {
        cout << "删除位置错误" <<endl;
        return;
    }
    q = p -> next;
    p -> next = q -> next;
    free(q);
}

int main()
{
    Node *first = CreatTailList();
    Node bufStudent;
    Node *pNew,*pCur = first;
    int nStudent;
    cout << "==========欢迎来到XXX系统=========="<<endl;
    cout << "请输入学生人数"  << endl;
    cin >> nStudent;
    cout << "请依次输入每个学生的学号,姓名,家庭住址"  << endl;
    for(int i=0; i<nStudent; i++)
    {
        pNew = (Node*)malloc(sizeof(Node));
        cin >> pNew->id >> pNew->name >>pNew->home;
        pCur->next = pNew;
        pCur = pNew;
    }
    while(1)
    {
        system("cls");
        cout << "输入将要进行的操作" << endl;
        cout << "1.删除某一编号的学生\n2.插入学生信息到某学生后面。\n3.打印学生信息\n4.退出" << endl;

        int Function;
        int FunctionN;
        cin >> Function;
        switch (Function)
        {
        case 1:
            cin >> FunctionN;
            Delete(first,FunctionN);
            PrintList(first);
            break;
        case 2:
            cout << "请输入要插入的学生信息"  << endl;
            cin >> bufStudent.id >> bufStudent.name >> bufStudent.home;
            cout << "请输入要哪个学生后面"  << endl;
            cin >> FunctionN;
            Insert(first,FunctionN,bufStudent);
            PrintList(first);
            break;
        case 3:
            PrintList(first);
            break;
        case 4:
            goto Out;
        default:
            cout << "输入不合法" << endl;
        }
        cout << "按任意键返回上一菜单" <<endl;

        system("pause");

    }
    Out:
    cout << "谢谢使用" << endl;
    return 0;
}

posted @ 2018-03-30 15:03  秃头大师  阅读(126)  评论(0编辑  收藏  举报