zoukankan      html  css  js  c++  java
  • 建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去

    建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去

    #include <stdio.h>
    #include <stdio.h>
    typedef struct student
    {
    	int num;
    	char sex[10];
    	char name[100];
    	int age;
    	struct student *next;
    } student;
    
    void printList(student *root)
    {
    	printf("----
    ");
    	while (root != NULL)
    	{
    		printf("num:%d, sex: %s, name: %s, age: %d
    ", root->num, root->sex, root->name, root->age);
    		root = root->next;
    	}
    }
    
    int main()
    {
    	student a[] = { { 1, "woman", "apple", 12 }, { 4, "woman", "banbana", 36 }, { 5, "man", "candy", 79 }, { 5, "man", "danny", 36 }, { 4, "man", "enjoy", 98 } };
    	for (int i = 0; i < 4; i++)
    	{
    		a[i].next = &a[i + 1];
    	}
    	a[4].next = NULL;
    
    	printList(&a[0]);
    
    	int n;
    	printf("请输入要删除的年龄:
    ");
    	scanf("%d", &n);
    	student *pre = a, *current = a->next, *head;
    
    	head = a;
    	while (current != NULL)
    	{
    		//如果头结点需要删除,则更新头结点
    		if (head->age == n)
    		{
    			pre->next = NULL;
    			pre = current;
    			current = current->next;
    			head = pre;
    		}
    		else
    		{
    			//删除节点,重新链接
    			if (current->age == n)
    			{
    				pre->next = current->next;
    			}
    			pre = current;
    			current = current->next;
    		}
    	}
    	printList(head);
    
    	return 0;
    }
    

    运行截图:

    建立一个链表,每个结点包括:学号、姓名、性别、年龄输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去

  • 相关阅读:
    43 李新佳 实验1
    0909 编译之路
    读后感
    评论
    0302作业
    构建之法第四章
    Latency Compensating Methods in Client/Server Ingame Protocol Design and Optimization
    利用Ptrace在Android平台实现应用程序控制[转]
    Bit Twiddling Hacks[转]
    Unity3D实用工具汇总[转]
  • 原文地址:https://www.cnblogs.com/cyuyanchengxu/p/13470603.html
Copyright © 2011-2022 走看看