C语言学习-数据结构 - 倒插法顺序表

时间:2023-03-10 00:05:51
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("依次输入链表数据\n");
while(p!=NULL){
printf("%c %d %d\n",p->data.name,p->data.x,p->data.y);
p = p->next;
}
printf("%\n\n");
} 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("\n请输入一个城市名,我们将返回它的坐标!\n");
fflush(stdin);
scanf("%c",&cityName);
cityNode = Locate(myList,cityName);
if(cityNode!=NULL){
printf("%d %d\n\n",cityNode->data.x,cityNode->data.y);
}else{
printf("未找到输入的城市!");
}
}