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("未找到输入的城市!");
	}
}

原文地址:https://www.cnblogs.com/dreamzhiya/p/6035639.html