Like Tree1Likes

Declaring linked list inside linked list

This is a discussion on Declaring linked list inside linked list within the C Programming forums, part of the General Programming Boards category; Originally Posted by blueboyz But I got realised now struct student and struct node is 2 linked list. The problem ...

  1. #31
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by blueboyz View Post
    But I got realised now struct student and struct node is 2 linked list. The problem was shown in the printlist function. Example : I have 2 node (a and b). And I want put student in node a. When I try to print list, student will also printed in node b. I can' get rid of it for now.
    Print list is similar to a print array function, it will print the entire list. If you want to print a single student you need to specify which student, find it and then print it. All this suggests a search function. Are you perhaps in the same class as this guy, but hasn't been able to formulate the problem as effectively? Link Lists of Structures within Structures...

    s-expressions looks conceptually very similar to syntax trees for representing language grammar, I'm not sure how that would help you.
    Last edited by Subsonics; 04-18-2012 at 08:45 AM.

  2. #32
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Subsonics View Post
    s-expressions looks conceptually very similar to syntax trees for representing language grammar, I'm not sure how that would help you.
    That is what they are for, conceptually.
    But it is a list whose nodes are either lists or just plain nodes, containing a value... (though the representation is like a tree or in terms of cons cells)
    Since that is (a generalization of) what the Original question asks, I think it can be adapted to this scenario.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #33
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by manasij7479 View Post
    That is what they are for, conceptually.
    But it is a list whose nodes are either lists or just plain nodes, containing a value... (though the representation is like a tree or in terms of cons cells)
    Since that is (a generalization of) what the Original question asks, I think it can be adapted to this scenario.
    A specific data structure also handles operations on nodes not just representation, otherwise what makes a syntax tree different from a regular binary tree. Arbitrarily complex datatypes can be made, but the question as stated doesn't mention the problem that needs to be solved.

  4. #34
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    How I wanna sorting arra struct inside struct. This is my code. I already changed struct student to array. kelas=node. I want to use qsort but I don't know what to do.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    typedef struct mhs mhs;
    typedef struct kelas kelas;
    
    
    struct mhs
    {
        char nama[31];
        char nim[16];
        int angkatan;
        float ipk;
    };
    
    
    struct kelas
    {
        char kls[13];
        int jml;
        mhs mhs[40];
        kelas *next;
    };
    
    
    kelas *head;
    kelas *phead;
    int nodeKelas;
    
    
    void init()
    {
        head=NULL;
        nodeKelas=0;
    }
    
    
    int menu()
    {
        int pil;
        printf("\t\t------------------------\n");
        printf("\t\t|Program Data Mahasiswa|\n");
        printf("\t\t------------------------\n\n\n");
        printf("1. Menambah kelas\n");
        printf("2. Memasukkan data mahasiswa\n");
        printf("3. Menghapus data mahasiswa\n");
        printf("4. Input data dari file\n");
        printf("5. Tulis ke dalam file\n");
        printf("Masukkan pilihan anda\t: ");scanf("%d",&pil);
        return pil;
    }
    
    
    
    
    void insertKelas(char *klas)
    {
       kelas *newNode=malloc(sizeof(kelas));
       if(newNode != NULL)
       {
          strcpy(newNode->kls,klas);
          newNode->jml=0;
          newNode->next=head;
          head=newNode;
          nodeKelas++;
       }
       else printf("Gagal menambah kelas");
       getch();
    }
    
    
    int cocokKelas(char *klas)
    {
        kelas *itr = head;
        while (itr != NULL)
        {
            if(strcmp(itr->kls,klas)==0)
            {
                phead=itr;
                return 1;
            }
            else itr=itr->next;
        }
    }
    
    
    
    
    int insertMhs(char *nama,char *nim,int angkatan,float ipk)
    {
        kelas *itr = phead;
        if(itr->jml<40)
        {
            int jmlMhs;
            jmlMhs=itr->jml;
            strcpy(itr->mhs[jmlMhs].nama,nama);
            strcpy(itr->mhs[jmlMhs].nim,nim);
            itr->mhs[jmlMhs].angkatan=angkatan;
            itr->mhs[jmlMhs].ipk=ipk;
            itr->jml++;
        }
        else printf("Jumlah Mahasiswa yang anda masukkan sudah terlalu banyak");
        getch();
    }
    
    
    static int sortByName(const void *a,const void *b)
    {
        mhs *ia = (mhs*)a;
        mhs *ib = (mhs*)b;
        return strcmp(ia->nama, ib->nama);
    }
    
    
    static int sortByNim(const void *a,const void *b)
    {
        const mhs *ia = a;
        const mhs *ib = b;
        return strcmp(ia->nim, ib->nim);
    }
    
    
    static int sortByAngkatan(const void *a,const void *b)
    {
        mhs *ia = (mhs *)a;
        mhs *ib = (mhs *)b;
        return (ia->angkatan-ib->angkatan);
    }
    
    
    static int sortByIpk(const void *a,const void *b)
    {
        mhs *ia = (mhs *)a;
        mhs *ib = (mhs *)b;
        return (int)(ib->ipk-ia->ipk);
    }
    
    
    void printList()
    {
        kelas *itr = head;
        while (itr != NULL)
        {
            printf("Kelas\t: %s",itr->kls);
            printf("Jumlah\t: %d\n\n",itr->jml);
            int i;
            for(i=0;i<itr->jml;i++)
            {
                printf("\t%d. Nama\t: %s\n",i+1, itr->mhs[i].nama);
                printf("\t   NIM\t: %s\n",itr->mhs[i].nim);
                printf("\t   Angkatan\t: %d\n",itr->mhs[i].angkatan);
                printf("\t   IPK: %.2f\n\n",itr->mhs[i].ipk);
            }
            itr = itr->next;
        }
    }
    
    
    void freeMemory()
    {
        kelas *deleteNode;
        while (head != NULL)
        {
            deleteNode = head;
            head = head->next;
            free(deleteNode);
        }
    }
    
    
    int main()
    {
        mhs kelasku[40];
        char tambahKelas[21],masukKelas[21];
       int pil;
       init();
       do
       {
            system("CLS");
            pil=menu();
            switch (pil)
            {
                case 1:
                    printf("Menambah kelas\t:");getchar();fgets(tambahKelas,21,stdin);
                    insertKelas(tambahKelas);
                    break;
                case 2:
                    printf("Memasukkan ke kelas\t:");getchar();fgets(masukKelas,21,stdin);
                    int jumlah;
                    if(cocokKelas(masukKelas)==1)
                    do
                        {
                            printf("Jumlah mahasiswa yang ingin anda masukkan di kelas %s\t",masukKelas);scanf("%d",&jumlah);
                            if (jumlah>40) printf("Jumlah mahasiswa yang ingin anda masukkan terlalu banyak");
                            else if (jumlah<=0) printf("Jumlah mahasiswa yang ingin anda masukkan tidak valid");
                            else
                            {
                                int i,angkatanMhs;
                                char namaMhs[31],nimMhs[16];
                                float ipkMhs;
                                for(i=0;i<40&&i<jumlah;i++)
                                {
                                    printf("%d. Nama\t: ",i+1);getchar();fgets(namaMhs,30,stdin);
                                    printf("   NIM\t: ");fgets(nimMhs,15,stdin);
                                    printf("   Angkatan\t: ");scanf("%d",&angkatanMhs);
                                    printf("   IPK\t: ");scanf("%f",&ipkMhs);
                                    insertMhs(namaMhs,nimMhs,angkatanMhs,ipkMhs);
                                }
                            }
    
    
                        }while(jumlah>40&&jumlah<=0);
                        break;
                case 3:
                    printList();
                    getch();
                    break;
                case 4:
                    qsort(mhs,40,sizeof(mhs),sortByNim);
                    printList();
                default:
                    break;
            }
       }while(pil>=1&&pil<4);
        freeMemory();
        return 0;
    }
    I already tried to change function insertmhsto be like this
    Code:
    int insertMhs(mhs *kelasku, char *masukKelas)
    {
        kelas *itr = phead;
        if(itr->jml<40)
        {
            int jumlah;
             printf("Jumlah mahasiswa yang ingin anda masukkan di kelas %s\t",masukKelas);scanf("%d",&jumlah);
                            if (jumlah>40) printf("Jumlah mahasiswa yang ingin anda masukkan terlalu banyak");
                            else if (jumlah<=0) printf("Jumlah mahasiswa yang ingin anda masukkan tidak valid");
                            else
                            {
             int i,angkatanMhs;
                                char namaMhs[31],nimMhs[16];
                                float ipkMhs;
    
    
            int jmlMhs;
            for(i=0;i<40&&i<jumlah;i++)
            {
                printf("%d. Nama\t: ",i+1);getchar();fgets(namaMhs,30,stdin);
                printf("   NIM\t: ");fgets(nimMhs,15,stdin);
                printf("   Angkatan\t: ");scanf("%d",&angkatanMhs);
                printf("   IPK\t: ");scanf("%f",&ipkMhs);
    
    
            jmlMhs=itr->jml;
            strcpy(itr->mhs[jmlMhs].nama,namaMhs);
            strcpy(itr->mhs[jmlMhs].nim,nimMhs);
            itr->mhs[jmlMhs].angkatan=angkatanMhs;
            itr->mhs[jmlMhs].ipk=ipkMhs;
            itr->jml++;
        }
        }
        }
        else printf("Jumlah Mahasiswa yang anda masukkan sudah terlalu banyak");
        getch();
    }
    This is how I called it in main
    Code:
     case 2:
                    printf("Memasukkan ke kelas\t:");getchar();fgets(masukKelas,21,stdin);
                    int jumlah;
                    if(cocokKelas(masukKelas)==1)
    
    
                                    insertMhs(kelasku,masukKelas);
    
     case 4:
                    structlen=sizeof(kelasku)/sizeof(mhs);
                    qsort(kelasku,structlen,sizeof(mhs),sortByName);
                    printList();
                    getch();
                    break;

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list inside a structure
    By jerepops in forum C Programming
    Replies: 5
    Last Post: 12-14-2009, 07:22 PM
  2. Linked List inside Linked List
    By de4th in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2006, 11:17 AM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21