| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 369 人关注过本帖
标题:结构体指针问题
只看楼主 加入收藏
狼族孤星
Rank: 1
等 级:新手上路
帖 子:11
专家分:3
注 册:2010-6-22
结帖率:50%
收藏
 问题点数:0 回复次数:1 
结构体指针问题
把if(i=1)修改下 就是搜索 防止重名写个链表 把搜出来的存入新建链表 为了删除方便 用了next  希望把删除的函数也给点指点




#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
#define len sizeof(struct memorydress)
struct student
{ char num[50];
  char score[20];     
  struct student *next;     
       };
struct memorydress{      
   struct student*p;   
  struct memorydress *next;     
       };           
      
      
  int n;
  struct student*creat(void)
  {struct student* head;
   struct student *p1,*p2;
   n=0;
   p1=p2=(struct student*) malloc(LEN);
   scanf("%s,%s",p1->num,p1->score);      
   head=NULL;   
   while((p1->num[0])!='0')
   {n=n+1;
    if(n==1)   head=p1;                  
    else  p2->next=p1;
    p2=p1;
    p1=(struct student*)malloc(LEN);
    scanf("%s,%s",p1->num,p1->score);                  
   }      
   p2->next=NULL;
   return(head);
         }
              
void print(struct student *head)//链表输出//
{struct student *p;
 printf("\nNow,These %d records are:\n",n);
 p=head;
 //if(head!=NULL)
   do
   {printf("%s,%s\n",(p->num),(p->score));
    p=p->next;           
     }while(p!=NULL);
}
/*struct student*insert(struct student *head,struct student*stud)
 {struct student *p0,*p1,*p2;
      p1=p2=head;
      p1=p1->next;
     p0=stud;
     p2->next=p0;
     p0->next=p1;
     n=n+1;
     return(head);            
   }   */
  struct memorydress* exas( struct student*head,char string[100],int i)//精确查找函数
{  int j=0;
   struct memorydress*q1,*q2;
   struct memorydress*head1;
   struct student* p3;
   p3=head;          //标记下
   if(i==1)
     { q1=q2=(struct memorydress*) malloc(len);
       head1=NULL;
      // while((p3->next)!=NULL)
          { if(strcmp(p3->num,string)==0)
             {q1->p=p3;
              while((q1->p)->num[0]!=0)
                {j=j+1;
                  if(j==1)   head1=q1;                  
                  else  q2->next=q1;
                        q2=q1;
                        q1=(struct memorydress*)malloc(len);
                        p3->next=p3;
                        q1->p=p3;
                }
                  q2->next=NULL;      
              }      
          }      
 
      
   return(head1);
     }
  else  if(i==2)
     { q1=q2=(struct memorydress*)malloc(len);
       head1=NULL;
      // while((p3->next)!=NULL)
          { if(strcmp(p3->score,string)==0)
             {q1->p=p3;
              while((q1->p)->score[0]!=0)
                {j=j+1;
                  if(j==1)   head1=q1;                  
                  else  q2->next=q1;
                        q2=q1;
                        q1=(struct memorydress*)malloc(len);
                         p3->next=p3;
                        q1->p=p3;
                }
                  q2->next=NULL;      
              }      
          }      
 
       }
   return(head1);
 }   
int main()
 {struct student *head,*stu;
  struct memorydress* q;
 // long del_num;
  
  char string[100];
  int i;
  char c;
  
  
  printf("input records:\n");   
  head=creat();
  print(head);      
  /*   printf("\ninput the inserted record:");
  stu=(struct student*)malloc(LEN);
  scanf("%s,%s",stu->num,stu->score);   
  while((stu->num[0])!='0')
  {
  head=insert(head,stu);
  print(head);
   printf("\ninput the inserted record:");
    stu=(struct student*)malloc(LEN);
  scanf("%s,%s",stu->num,stu->score);
  } */
   printf("输入查找方式:1   按num查找  ; 2   按score查找; 请输入:");
  scanf("%d",&i);
   printf("输入要查找的对象:");
   c=getchar();
  gets(string);      
  q=exas(head, string, i);  
 while(q->next!=NULL)
   // printf("%s,%s\n",(q->p)->num,(q->p)->score);
   { puts((q->p)->num);
     puts((q->p)->score);
     q->next=q;
}   
  system("pause");
}
搜索更多相关主题的帖子: 指针 结构体 
2010-09-06 14:24
狼族孤星
Rank: 1
等 级:新手上路
帖 子:11
专家分:3
注 册:2010-6-22
收藏
得分:0 
斑竹们,来吧!!!!
2010-09-06 14:28
快速回复:结构体指针问题
数据加载中...
 
   



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

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