CTDL & GT - List

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct data

{

         char ht[25];

         char lop[5];

         int tuoi;

} data;

typedef struct node

{

        data info;

        struct node *next;       

} node;

typedef node *list;

list head,top;

void input(data &p)

{

fflush(stdin);

 printf("

Nhap Vao Ho Ten: "); gets(p.ht);

 printf("

Nhap Vao Lop: "); gets(p.lop);

 printf("

Nhao Vao Tuoi: "); scanf("%d",&p.tuoi);  

}

node  *getnode(list &head,list &top, data q)

{

  node *p;

  p=(node*)malloc(sizeof (node));

  if (p==NULL){

               printf("

Day bo Nho");

               exit(1);

               }

  p->info = q;

  p->next=NULL;

  return p;

}

void insertfirst (list &head,list &top, node *p)

{

     if (head ==NULL )

     {

             head =p;

             top=p;

             }    else {

                          p->next =head;

                          head =p;

                       }

}

void inds (list head)

{

     printf("

Danh Sach La: ");

     node *p;

     p=head;

     while (p!=NULL ) {

                       printf("

HoTen: %25s\t Lop: %10s \t Tuoi: %3d ",p->info.ht,p->info.lop,p->info.tuoi);

                       p=p->next;

                      }

}

void insertend (list &head,list &top, node *p)

{

     if (head==NULL)

        {

                    head =p;

                    top=p;

                    } else {

                           top->next=p;

                           top=p;

                           }

     }

node *search (list &head,list &top,char *key)

{

     node *p,*temp;

     p=head;

     while (p != NULL )

     {

           if(strcmp(p->info.ht,key)== 0) return p;

           p=p->next;  

           }

     return NULL;

}

int main(int argc, char *argv[])

{

    head =NULL;

    top=NULL;

    data p;

    node *q;

    int n,i;

    printf("

Nhap Vao N: "); scanf("%d",&n);

    for (i=1;i<=n;i++)

    {

        input(p);

        q=getnode (head,top,p);

        insertfirst(head,top,q);

        }

    inds (head);

    q = search (head,top,"hung");

    if (q!=NULL) printf("

Tim Thay:

HoTen: %25s\t Lop: %10s \t Tuoi: %3d ",q->info.ht,q->info.lop,q->info.tuoi);

  printf("

");

  system("PAUSE");   

  return 0;

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: