zoukankan      html  css  js  c++  java
  • 单向链表的创建、输出、插入、删除

    #include <iostream>
    using namespace std;
    struct Node {
           int id;
           int score;
           struct Node *next;	
     }*list_head;
    void scan()
    {
        int id;
        Node *list_temp;		
        list_head = NULL;	
    	list_temp = list_head;	
    	cout <<  "输入id和score:" << endl; 
        while(cin >> id,id)
        {
            Node *list_body = new Node();		
            list_body->id = id;
            cin >> list_body->score;
            list_body->next = NULL;
            if(list_head == NULL)	
                list_head = list_body;
            else
                list_temp->next = list_body;
            list_temp = list_body;
        }
    }
    void print()
    {
    	Node *list_print;
    	list_print = list_head;
    	while(list_print)
    	{
    		cout << "----------------------------" << endl;
    		cout << "学生ID:" << list_print->id << endl;
    		cout << "学生分数" << list_print->score << endl;
    		cout << "----------------------------" << endl;
    		list_print=list_print->next;		
    	}
    }
    void insert()
    {
    	int front_id;
    	Node *list_insert = new Node();
    	Node *list_temp = list_head; // 遍历链表的过程节点 
    	cout << "输入插入的学生信息前一位的学生ID" << endl;
    	cin >> front_id;
    	while(list_temp && list_temp->id != front_id)
    	{
    		list_temp = list_temp->next; 
    	} // 找到插入位置前一位的学生信息链表 
    	cout << "输入插入的学生id" << endl;
    	cin >> list_insert->id; 
    	cout << "输入插入的学生成绩" << endl;
    	cin >> list_insert->score;
    	list_insert->next = list_temp->next;
    	list_temp->next = list_insert;
    }
    void delete_id()
    {
    	int delete_id;
    	Node *list_delete = list_head; // 要删除的节点 
    	Node *list_front = list_head;
    	cout << "请输入要删除学生信息的学生ID" << endl;
    	cin >> delete_id; 
    	while(list_delete && list_delete->id != delete_id)
    	{
    		list_front = list_delete;
    		list_delete = list_delete->next;
    	}
     	list_front->next = list_delete->next;
    	free(list_delete);
    }
    int main()
    {
    	scan();
    	print();
    	insert();
    	print();
    	delete_id();
    	print();
    	return 0;
    }
    
  • 相关阅读:
    WINFORM实现进程信息的查看,listview,点击,右键,右键菜单
    VFP 实验参考答案
    C++ 大作业 超市收银系统
    ASP实现计算机爱好者网站,可以直接浏览
    C语言实验单片机串口发送int型数据
    超声波 HC-SR04
    django应用之corsheaders[跨域设置]
    git
    Your activation code could not be validated(error 1653219)
    Django Rest Framework简介及初步使用
  • 原文地址:https://www.cnblogs.com/kkyblog/p/11264180.html
Copyright © 2011-2022 走看看