单向链表的创建、输出、插入、删除

#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;
}
原文地址:https://www.cnblogs.com/kkyblog/p/11264180.html