zoukankan      html  css  js  c++  java
  • C语言学习-数据结构

    // test20161106.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stdlib.h"
    #include "conio.h"
    
    typedef struct{
    	char name;
    	int x;
    	int y;
    }ElemType;
    
    typedef struct Node{
    	ElemType data;
    	struct Node *next;
    }Node,*LinkList;
    
    void InitList(LinkList *L){
    	*L=(LinkList)malloc(sizeof(Node));
    	(*L)->next = NULL;
    }
    
    void DisplayList(LinkList L){
    	Node *p;
    	p = L->next;
    
    	printf("依次输入链表数据
    ");
    	while(p!=NULL){
    		printf("%c %d %d
    ",p->data.name,p->data.x,p->data.y);
    		p = p->next;
    	}
    	printf("%
    
    ");
    }
    
    void CreateFromHead(LinkList L){
    	Node *s;int flag=1;char c;
    	char inputName;int inputX=0;int inputY=0;
    	while(flag){
    		fflush(stdin);
    		scanf("%c %d %d",&inputName,&inputX,&inputY);
    		if(inputName!='$'){
    			s=(Node*)malloc(sizeof(Node));
    			s->data.name=inputName;
    			s->data.x=inputX;
    			s->data.y=inputY;
    			s->next=L->next;
    			L->next=s;
    			L = s;
    		}else{
    			flag = 0;
    			L->next = NULL;
    		}
    	}
    }
    
    Node *Locate(LinkList L, char key){
    	Node *p;
    	p=L->next;
    
    	while(p!=NULL){
    		if(p->data.name!=key){
    			p=p->next;
    		}else{
    			break;
    		}
    	}
    	return p;
    }
    
    
    void _tmain(int argc, _TCHAR* argv[])
    {
    	LinkList myList;
    	char cityName;
    	Node *cityNode;
    	int positionX,positionY;
    	float distance;
    	int flag,tag;
    
    	//初始化链表
    	InitList(&myList);
    
    	//头插法建链表
    	CreateFromHead(myList);
    
    	//显示链表
    	DisplayList(myList);
    	getch();
    
    	printf("
    请输入一个城市名,我们将返回它的坐标!
    ");
    	fflush(stdin);
    	scanf("%c",&cityName);
    	cityNode = Locate(myList,cityName);
    	if(cityNode!=NULL){
    		printf("%d %d
    
    ",cityNode->data.x,cityNode->data.y);
    	}else{
    		printf("未找到输入的城市!");
    	}
    }
    
    
  • 相关阅读:
    【11平台天梯】【原理分析】11平台天梯原理分析
    2020年8月11日
    2020年8月10日
    2020年8月12日
    2020年8月9日
    2020年8月13日
    2020年8月8日
    2020年8月7日
    2020年8月6日
    2020年8月14日
  • 原文地址:https://www.cnblogs.com/dreamzhiya/p/6035639.html
Copyright © 2011-2022 走看看