⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 01.c

📁 实现链表的数据读入
💻 C
字号:
/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "math.h"

#define radius 2
typedef int datatype;
typedef struct node
{
    datatype data[2];
    struct node * next;
}linklist;




paint(linklist *p)
{
    int i;
    linklist *head;
    static int x=1;
    setcolor(x++);
    head=p;
    while(p->next!=NULL)
    {
    circle(p->data[0],p->data[1],radius);
    moveto (p->data[0],p->data[1]);
    p=p->next;
    lineto(p->data[0],p->data[1]);
    }
    circle(p->data[0],p->data[1],radius);
    return head;
}




readtxt()
{
 int a,b;
 linklist * head,*s,*tail;
 FILE *fp;
 fp=fopen("data02.txt","r");
 if(fp==NULL){printf("There is no datatext!\n");return(0);}
 head=NULL;tail=NULL;
 fscanf(fp,"%d,%d\n",&a,&b);
 while(!feof(fp))
 {
    fscanf(fp,"%d,%d\n",&a,&b);
    s=(linklist*)malloc(sizeof(linklist));
    s->data[0]=a;s->data[1]=b;
    if(head==NULL) head=s;
    else tail->next=s;
    tail=s;
  }
  if(tail!=NULL) tail->next=NULL;
  return head;
}


getlocation(p,n)
{
    int k;
    linklist *l;
    l=p;
    for( k=1;k<n-1;k++) l=l->next;
    return l;
}

insert(linklist * p)
{
 int n;
 linklist * head;
 linklist * newnumber;
 head=p;
 newnumber=(linklist*)malloc(sizeof(linklist));/*定义了newnumber为listlink型,为什么还要分配空间*/
 printf("please input the number's X,Y you want to insert:\n");
 scanf("%d,%d",&newnumber->data[0],&newnumber->data[1]);
 printf("please input the location you want to insert the newnumber:\n");
 scanf("%d",&n);
 if(n==1)
 {newnumber->next=p;head=newnumber;}
 else
 {p=getlocation(p,n);
 newnumber->next=p->next;
 p->next=newnumber;}
 return head;
}

deletedata(linklist *p)
{
    linklist *head,*q;
    int k,n;
    head=p;
    printf("please input the number's location you want to delete:\n");
    scanf("%d",&n);
    if(n==1) {head=p->next;free(p);}
    else if((p!=NULL) && (p->next!=NULL))
    {p=getlocation(p,n);
    q=p->next;
    p->next=q->next;
    free(q);}
    return head;
}



searchdata(linklist *p)
{
   linklist *head;
   int n;
   head=p;
   printf("please input the number's location:\n");
   scanf("%d",&n);
   getlocation(p,n);
   if(n==1)
   {printf("X,Y:%d,%d\n",p->data[0],p->data[1]);circle(p->data[0],p->data[1],radius*2);}
   else{
   p=getlocation(p,n);
   printf("X,Y: %d,%d\n",p->next->data[0],p->next->data[1]);
   circle(p->next->data[0],p->next->data[1],radius*2);}
   return head;
}

main()
{
   linklist *p ;
   int flag;
    int gdr=DETECT,gmd=0;
    initgraph(&gdr,&gmd,"");
    p=readtxt();  p=paint(p);
    printf("insert a new number--1\ndelete a number--2\nsearch a number's X,Y--3\nexit--4\n");
    scanf("%d",&flag);
    while(flag!=4)
    {
        switch(flag)
        {
        case 1:
            p=insert(p);p=paint(p);
            break;
        case 2:
            p=deletedata(p);p=paint(p);
            break;
        case 3:
            p=searchdata(p);
            break;
        case 4:
            exit (0);
            break;
        }
    printf("please choose the menu:");
    scanf("%d",&flag);
    }
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -