| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 776 人关注过本帖
标题:请教高手:帮忙看一下,急~~建立链表,文件写入链表并printf打印出来??
只看楼主 加入收藏
henyue
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-6-3
收藏
 问题点数:0 回复次数:1 
请教高手:帮忙看一下,急~~建立链表,文件写入链表并printf打印出来??
//循环双向链表 排序 数据源(文件读取)结果
//建立一个双向循环链表,把文件里的数据读到链表中,链表降序排序并用printf打印出来。

#include <stdio.h>                                               
#include <stdlib.h>   
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>

#define SIZE 5
#define LEN sizeof(struct d_list)

struct d_list
{                                                  
   int data;                                                     
   struct d_list *prior;                                          
   struct d_list *next;                                            
}* head;  
         
#if 0               
void create_d_list ( struct d_list **headp, int *p )                                                                                                                                                                  
{                                                               
   struct d_list *head = NULL, *tail;                           
   if( p[0] == 0 )                                                     
   *headp = NULL;                                                   
   else
   {                                                           
      head = tail = ( struct d_list * ) malloc ( LEN );            
      //head->data = *p++;   
      head->data = *p;                                             
      tail = head;
      printf("head->data = %d\t",head->data);                                                
      //while( *p )
      while ( head->data != 0 )
      {                                                      
         tail->next = ( struct d_list * ) malloc ( sizeof (struct d_list) );   
         tail->next->prior = tail;                                       
         tail = tail->next;                                                                                          
         tail->data = *p;
      }
      printf("tail->data = %d\n",tail->data);                          
      tail->next = head;      
      head->prior = tail;                                             
   }
   *headp = head;                                                   
}
#endif

/*创建双向循环链表
 *函数名:create_d_list
 *参数:n:带n个结点
 *返回值:链表头指针     */   
struct d_list * create_d_list(int n)
{
    struct d_list *p, *s;
    int i;
    p = head = ( struct d_list * ) malloc ( LEN );
    for( i = 1; i <= n; i++ )
    {
      s = ( struct d_list * )malloc( LEN );
        //scanf( "%d", &s->data );
        s->prior = p;
        p->next = s;
        p = s;
    }
    p->next = head; head->prior = p;
    return head;
}

/*解析内容
 *函数名: spit
 *参数:buf:要分解的字符串;q:分隔符字符串
 *返回值:0:成功;  1:失败 */
int spit( unsigned char *buf )
{    
   char *p, *q = ",";
   int n;
   p = strtok( buf, q );
   //printf( "%s %d\n",__FILE__,__LINE__ );
   while( p != NULL )
   {
         printf( "%s\n", p );  
      create_d_list(n);
         p = strtok( NULL, q );
      //printf( "%s %d\n",__FILE__,__LINE__ );
         printf( "%s\n", p );
         p = strtok( NULL,q );
   }
   //printf( "%s %d\n",__FILE__,__LINE__ );
   return 0;
}

/*写文件
 *函数名:write_buf
 *参数:filename:文件名; buf:存入数据的首地址; len:数据块的字节数   
 *返回值:成功:len; 失败:0 */                  
int write_buf( char *filename, unsigned char *buf, int len )
{
     int ret,handle;
     char msg[20];  
   if( filename == NULL || buf == NULL )
      return -1;
   if((handle = open("file1", O_RDWR)) == -1 )
   {
      printf( "cannot open the file\n" );
      return -2;     
   }
   //ret = write( handle, dlist, sizeof( dlist ) );
   ret = write( handle, msg, strlen(msg) );
   if( ret != 1)
         printf( "write error\n" );             
   close(handle);
   return ret;
}                    

/*遍历双向循环链表 写文件     
 *函数名:disp_d_list
 *参数: p1:结构体类型的指针
 *返回值:无                    */        

void disp_d_list ( struct d_list *p1 )                             
{                                                               
   struct d_list *p = p1;
   char buffer[50];
   //int i=1;
   int handle;
   
   if( (handle =open("file1",O_RDWR)) == -1 )
      return;   
   do{
          memset( buffer, 0x00, sizeof(buffer) );
          sprintf( buffer, "%d", p1->data);
       //write( handle,buffer,strlen(buffer) );      
       /*if( ((i % 4) == 0) )
             write( handle,"\n",1 );      
       i++;*/      
       //printf( "%d\n", p->data );
       p = p->next;
   } while( p != p1 );   
   //write_buf( stream, buffer, strlen(buffer) );
   close(handle);
}  

/*读一行
 *函数名:readn
 *参数:  fd:文件句柄; buf:存入数据的首地址
 *返回值: i  */
ssize_t readn( int fd, char *buf )
{
    int len = 0, i = 0;
    
    while( 1 )
    {
        if( (len = read( fd, &buf[i], 1 ) ) <= 0 )
            return len;
        //if( buf[i] == '\n' )
        if( buf[i] == 0x0D )   //ASCII=13,回车,将当前位置移到本行开头
            break;
        i++;
    }
    printf( "%s buf = %s\n", __func__, buf );    
    return i;
}

/*读文件(读内容、解析内容)     
 *函数名:Get_File_Content
 *参数:  filename
 *返回值: 成功 0; 失败 -1  */
int Get_File_Content( char *filename )
{
     char buf[8192];        
     int handle;
     //int m, k;
     if( filename == NULL )
          return -1;          
   if( ( handle = open( filename, O_RDWR ) ) == -1 )
          return -1;        
     memset( buf, 0x00, sizeof(buf) );    
   //printf( "%s %d\n",__FILE__,__LINE__ );
   while( 1 )
     {
           int len;
           
           if( (len = readn ( handle, buf )) >  0 )
           spit( buf );
        else
             break;  
        //sleep(1);
       
        #if 0
      for( m = 0; m < k; m++ )
         if( buf[m] >= '0' && buf[m] <= '9' )
              printf("%c",buf[m]);
      printf( "\n" );
      #endif
      
   }
   //printf( "%s %d\n",__FILE__,__LINE__ );
     close( handle );
     return 0;
}
  
/*双向循环链表降序排列     
 *函数名:sort_d_list
 *参数:
 *返回值:     */                                                                                      
void sort_d_list( struct d_list **headp1 )
{        
   struct d_list *p, *q;                                            
   int i,j,k,n = 0;                                                  
   p = *headp1;                       
   //printf( "%s %d\n",__FILE__,__LINE__ );           
   while( p != (*headp1)->prior )
   {                                    
      n++;                                                            
      p = p->next;
   }      
   //printf( "%s %d\n",__FILE__,__LINE__ );                     
   for( i = 0, p = *headp1; i < n; p = p->next, i++ )                           
   for( j = i, q = p->next; j < n; q = q->next, j++ )                           
   if( (p->data) < (q->data) )
   {                                       
      k = q->data;                                                      
      q->data = p->data;                                                
      p->data = k;                                                      
   }   
}                       

int main( int argc, char *argv[] )
{                                                            
   Get_File_Content( "num" );                                                                                                                                                                                                                                                                                                                                                                                                   
   sort_d_list( &head );                              
   disp_d_list( head );                        
   return 0;               
}

[[it] 本帖最后由 henyue 于 2008-8-22 12:37 编辑 [/it]]
搜索更多相关主题的帖子: 链表 printf 文件 打印 
2008-08-21 19:58
henyue
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-6-3
收藏
得分:0 
怎么没人帮忙看一下啊?急我~~
2008-08-22 12:24
快速回复:请教高手:帮忙看一下,急~~建立链表,文件写入链表并printf打印出来? ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032149 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved