自己玩,別鬧

來源:互聯網
上載者:User

/***
*        Copyright (C) 1998-2003, 2004, 2005 Free Software Foundation, Inc.
*    This file is part of the GNU C Library.
* 
*    The GNU C Library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation; either
*    version 2.1 of the License, or (at your option) any later version.
* 
*    The GNU C Library is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
* 
*    You should have received a copy of the GNU Lesser General Public
*    License along with the GNU C Library; if not, write to the Free
*    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
*    02111-1307 USA.
*     designed by Shuai,bai
****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define string char*
#define DESC   50
#define POWER  100
#define ROUTE  0
#define NODE   1
#define Max_Node 9
#define TRUE 1
#define FALSE 0

typedef struct Node
{
 int id;                 //Each sight has a only id
 char desc[DESC];        //Each sight has a description
 int power;  //Between two sight,there is a power of distence
 struct Node *next; // The next Node
}Node;
typedef struct Route
{
 int id;
 char desc[DESC];
 int power;
 struct Node *node;
 struct Route *next;
}Route;

static string base_table[] = {"111","222","333","444","555","666","777","888"};
static string route_table[] = {"wo","shi","yi","ge","da","sha","bi","!","!"};
static Node *result[Max_Node] = {};

static Node *node[Max_Node];
/**
* function:
* Node *InitList_Node(Node *Head,int flag)
*
* description:
*   Init  Node List,return the head of node list which has been initialed.
**/
Node *InitList_Node(Node *Head)
{
 //do initial
 Head = (Node*)malloc(sizeof(Node));
 Head->id = 0;
 Head->next = NULL;
 memset(Head->desc,0,sizeof(*Head->desc));
 return Head; 
}
/**
* function:
*  Node *InitList_Route(Route *Head)
* description:
* Init Routelist,return the head of route list which has been initialed.
**/
Route *InitList_Route(Route *Head)
{
 //do initial
 Head = (Route *)malloc(sizeof(Route));
 Head->id = 0;
 Head->node = NULL;
 Head->next = NULL;
 
 return Head;
}

/**
* function:
* Node *CreateListTail_Node(Node *head,int count,string base[])
* description:
* Create Node list
*
**/
Node *CreateListTail_Node(Node *head,int count,string base[])
{
 Node *s,*r;
 r = head;
 int i = 0;
 while(0 != count)
 {
  s = (Node *)malloc(sizeof(Node));
  strcpy(s->desc,base[i]);
  s->id = i++;
  s->power = POWER;
  
  s->next = r->next;
  r->next = s;
  r = s;
  count --;
 }
 r->next = NULL;
 return head;
}
/**
* function:
* Node *CreateListTail_Route(Node *head,int count,string base[])
* description:
* Create Route list,count is number of node
***/
Route *CreateListTail_Route(Route *head,int count,string base[])
{
 Route *s,*r;
 r = head;
 int i = 0;
 while(0 != count)
 {
  s = (Route *)malloc(sizeof(Route));
  Node *n = node[i];
  s->node = n;
  strcpy(s->desc,base[i]);
  s->node = node[i]; //each Route's node is a node list
  s->id = i++;
  s->power = POWER;
  s->next = r->next;//NULL
  r->next = s;
  r = s;
  count --;
 }
 return head;
}

string strcpp(string base[],string str)
{ 
 *str = *base[1];
 return str;
}
/**
*  function:
*  void printList_Node(Node *h);
*  description:
*    print Node List,just used to test
**/
void printList_Node(Node *h)
{
 Node *s = h;
 while(s->next!= NULL)
 {
  if(strcmp("",s->desc))
  {
   printf("[%s]\t",s->desc);
  }
  s = s->next;
 }
 printf("\n");
}
/**
*  function:
*  void printList_Route(Route *h)
*  description:
*    print Route List,just used to test
**/
void printList_Route(Route *h)
{
 Route *s = h;
 Node *n = h->node;
 while(s->next != NULL)
 {
  printf("[%s]\t",s->desc);
  while(n->next != NULL)
  {
   puts(n->desc);
   n = n->next;
  }
  s = s->next;
 }
 printf("\n");
}

/**
* function
*       Node *doSearch(Node* h)
* description
*  search the destination,and return the list of find path(Node*)
**/
Node *doSearch(Route *h,string desc)
{
 Route *st = h;
 Node *temp = InitList_Node(temp);
 Node *nr;
 nr = temp;
 int flag = 0;//flag to refer findout or not
 //while(st->next != NULL)
 do
 {
  Node *ns;
  if(0 != strcmp(desc,st->desc))//if not find the node
  {
   ns = (Node *)malloc(sizeof(Node));
   ns->id = st->id;
   ns->power = st->power;
   strcpy(ns->desc,st->desc);
   ns->next = nr->next;//NULL
   nr->next = ns;
   nr = ns;
   printf("[%s]\t",nr->desc);  
   flag = 1;//not find
  }else if(0 == strcmp(desc,st->desc))//if findout,link the  last node at end of the temp
  {
   ns = (Node *)malloc(sizeof(Node));
   ns->id = st->id;
   ns->power = st->power;
   strcpy(ns->desc,st->desc);
   ns->next = nr->next; 
   nr->next = ns;
   puts(nr->next->desc);
   ns->next = NULL;
   flag = 0;//find out
   break;
  }
   st = st->next;
 }while(st->next != NULL);
 return temp;
}
/*
Node *doSearch_temp(Route *h,string desc)
{
 //Node *result[]; //define result buffer
 Route *st = h;   //define s to recieve route head
 static int i = 0;
 static int flag = 0;
 while(st->next != NULL)
 {
  //Initial result header
  //result[i] = (Node *)malloc(sizeof(Node));
  //result[i]->id    = 0;
  //result[i]->desc  = NULL;
  //memset(result[i],0,DESC);
  //result[i]->power = 0;
  //result[i]->next  = NULL;
   Node *nr,*ns;
         nr               = result[i];  
  //while(strcmp(s->desc,desc))
  if(!strcmp(st->desc,desc))//Route list can not find,then find the Node List
  {
   //if not find, link the every node to the result
   ns        = (Node *)malloc(sizeof(Node));
   ns->id    = st->id;
   ns->power = st->power;
   //ns->desc  = s->desc;
   strcpy(ns->desc,st->desc);
    
   ns->next  = (Node *)st->next;
   nr        = ns;
  
   while(st->node->next != NULL)//find the Node list
   {
    // if can not find the desc
    if(!strcmp(st->node->desc,desc))
    {
     Node *ret = st->node;
     Node *sr  = (Node *)malloc(sizeof(Node));
     sr->id    = st->node->id;
     sr->power = st->node->power;
     //sr->desc  = s->node->desc;
     strcpy(sr->desc,st->node->desc);
     
     ns->next  = sr->next;
     nr        = sr;
     st->node    = st->node->next;  
     flag      = 0;
    }else //else find the desc,search will continue
    {
     //i++;
     flag = 1;//find flag
     break;
    }
   
   }

   if(flag)//if find out,i++,otherwize,do not change i
   {
    i++;
    flag = 0;
    break;
   }
   st = st->next;

  }else{//else Find out in Route list
   i++;
   break;
  } 
 } 
 return result[i];
}
*/
void Init()
{
 int i;
 for(i=0;i<Max_Node;++i)
 {
  result[i] = InitList_Node(result[i]);
 }
}

void doFree(Node *h)
{
 if(NULL == h)
 {
  return;
 }
 Node *temp = h->next;
 while(temp->next != NULL)
 {
  free(h);
  temp = temp->next;
 }
}

void FindOut(Route *h,string desc)
{
 Route *st = h;
 int counter = 0;
 bool flag = FALSE;
 Node *temp = doSearch(h);
 if(NULL != temp)
 {
  result[counter++] = temp;
 }
 Node *nr,*sr,*nt,*ns;//nr,sr to link the node list,nt refer to Route list's node which is head of Node list,ns to save the temp node list of head
 while(st->next != NULL)
 {
  if(0 != strcmp(desc,st->desc))
  {
   nr = result[counter];
   sr = (Node *)malloc(sizeof(Node));
   sr->id = st->id;
   sr->power = st->power;
   strcpy(sr->desc,st->desc);
   
   sr->next = nr->next;//NULL
   nr->next = sr;
   nr = sr;
   
   nt = st->next->node;
   ns = nt;
   while(nt->next != NULL)
   {
    if(0 != strcmp(desc,nt->desc))
    {
     sr = (Node *)malloc(sizeof(Node));
     memcpy(sr,nr,sizeof(Node));
     
     sr->next = nr->next;
     nr->next = sr;
     nr = sr;
     flag = FALSE;
    }else{
     sr = (Node *)malloc(sizeof(Node));
     memcpy(sr,nr,sizeof(Node));
     sr->next = nr->next;
     nr->next = sr;
     nr = sr;
     flag = TRUE;
     counter++;
     break;
    }
    nt = nt->next;
   }
   if(!flag)
   {
    doFree(ns);
   } 
   //st = st->next;
  }else{
   sr = (Node *)malloc(sizeof(Node));
                                sr->id = st->id;
                                sr->power = st->power;
                                strcpy(sr->desc,st->desc);
                               
                                sr->next = nr->next;//NULL
                                nr->next = sr;
                                nr = sr;
                               
                                nt = st->next->node;
                                ns = nt;
  }
  st = st->next;
 }
}

int main()
{
 //Init Node list
 int i = 0;
 for(; i<Max_Node; i++)
 {
  node[i] = InitList_Node(node[i]);
  CreateListTail_Node(node[i],8,base_table);
 }
 
 //Create route linklist
 Route *route = InitList_Route(route);
 CreateListTail_Route(route,9,route_table);
 
 Init();
 
 //print the hole List of Route
 Route *rt = route;
 while(rt->next != NULL)
 {
  Node  *nt = route->next->node;
  printf("[%s]\t",rt->desc);
  while(nt->next != NULL)
  {
   printf("[%s]\t",nt->next->desc);
   nt = nt->next;
  }
  rt = rt->next;
  printf("\n");
 }
 printf("\n");
 //do researching for the Route List ,value is yi;
 Node *test =  doSearch(route,"bi");
 Node *ntest = test;
 printList_Node(test);
 //print(result);
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.