Thursday, March 31, 2011

Linked List dan Structure

Minggu ni kelas Data Structure aku ada Lab Test. Dalam lab test tu kitorang kena modify program linked list yang dia berikan mengikut kehendak soalan. Linked list kena gunakan structure untuk buat yang mana structure gunakan pointer. Aku pun masih blur2 lagi pasal pointer dengan structure ni, tapi apa yang aku paham kalo tak paham structure memang linked list pun tak kan paham. Tapi sebab aku ada anugerah yang telah Allah S.W.T berikan, iaitu anugerah pandai meng'edit', maka dapat r aku selesai kan programming ni. Untuk belajar programming ni, kita kena selalu gunakan konsep 'trial and error' , baru r kita boleh paham macam mana flow programming tu. Kita guna 'coding' ni, tengok macam mana output dia, pahamkan macam mana compiler tu baca instruction yang kita buat "line by line". Kalo kita reti baca coding tu baris demi baris, maka kita dapat cari dekat mana 'error' yang kita buat. Dekat bawah ni adalah soalan Lab Test minggu ni, setelah bertungku-lumus, akhirnya aku berjaya 'solve' kan juga.

# include <stdio.h>
# include <stdlib.h>
struct node
    {
    int data;
    struct node *link;
    };
struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    if(p==NULL)
        {
        p=(struct node *)malloc(sizeof(struct node));
        if(p==NULL)
            {
            printf("Error\n");
            exit(0);
            }
        p-> data = n;
        p-> link = NULL;
        }
    else
        p->link = insert(p->link,n);/* the while loop replaced by
                                    recursive call */
    return (p);
    };
void printlist ( struct node *p )
    {
    printf("The data values in the list are\n");
    while (p!= NULL)
        {
        printf("%d\t",p-> data);
        p = p-> link;
        }
    };
void main()
    {
    int n;
    int x;
    struct node *start = NULL ;
    printf("Enter the # of nodes to be created \n");
    scanf("%d",&n);
    while ( n--> 0 )
        {
        printf( "Enter the data values to be placed in a node\n");
        scanf("%d",&x);
        start = insert ( start, x );
        }
    printf("The created list is\n");
    printlist ( start );
    }
    /*
    QUESTION:
    a)  modify the above program output the list every time
        node data is entered.
        e.g:
        34
        34 -->
        50
        34 --> 50 -->
    b)  modify the program so as to find the maximum and
        minimum node values in the list.
    c)  modify the program so as to reverse the linked list.

Kalo korang tengok soalan tu, dia ada satu structure untuk programkan linked list, satu function untuk display dan satu main program. Aku pun still tak paham lagi macam mana guna structure tu tapi aku tau structure tu digunakan untuk membaca dan susun data yang korang akan masukkan masa kat main program tu nanti kepada linked list. Kalo korang tengok jawapan coding aku untuk soalan A,B dan C. Aku gunakan struktur yang sama untuk semua program yang mana ada satu structure, satu function display dan satu main program (dah nama pun suruh modify, kalo dah lain bebenor, tu bukan modify namanya). Sebab apa? sebab memang tu coding untuk buat linked list. Memang ada cara lain lagi kot atau mungkin jauh beza dari coding tu, tapi kalo korang setkan dalam mind set korang yang coding tu memang guna untuk cari linked list, maka korang boleh gunakan structure yang sama untuk suma soalan, sebab sumanya dalah untuk cari linked list. Macam matematik jugak, equation bawah ni korang boleh dengan macam2 cara:-
2+2=4 atau
(+2)+2=4 atau
2+(+2)=4 atau
2+2=+4

bentuk yang berbeza tetapi semuanya benda yang sama. Macam tu jugak r programming, macam2 coding kita boleh buat dan gunakan untuk 'solve'kan sesuatu soalan asalkan tak lari dengan apa yang kita nak cari. Untuk soalan A, dia nak:-

  a)  modify the above program output the list every time
        node data is entered.
        e.g:
        34
        34 -->
        50
        34 --> 50 -->

Perlu tak aku gunakan coding lain? tak perlu sebab dia cuma nak output tu je yang display kan benda lain. So nak modify kat mana? modify kat bahagian function display ngan kat main program. Just cut and paste jer.

# include <stdio.h>
# include <stdlib.h>
struct node
    {
    int data;
    struct node *link;
    };
struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    if(p==NULL)
        {
        p=(struct node *)malloc(sizeof(struct node));
        if(p==NULL)
            {
            printf("Error\n");
            exit(0);
            }
        p-> data = n;
        p-> link = NULL;
        }
    else
        p->link = insert(p->link,n);/* the while loop replaced by
                                    recursive call */
    return (p);
    };

void printlist ( struct node *p )
    {
    while (p!= NULL)
        {
        printf("%d --> ",p-> data);
        p = p-> link;
        }
    };

void main()
    {
    int n;
    int x;
    struct node *start = NULL ;
    printf("Enter the # of nodes to be created \n");
    scanf("%d",&n);
    while ( n--> 0 )
        {
        printf( "Enter the data values to be placed in a node\n");
        scanf("%d",&x);
        start = insert ( start, x );
        printlist ( start );
        }
    printf("\n\nThe created list is\n");
    printlist ( start );
    printf("GND");
    }
Untuk soalan b pulak, dia nak:-   

b)  modify the program so as to find the maximum and
        minimum node values in the list.
Untuk soalan ni aku ada dua coding satu yang 'sort' dan satu lagi 'unsort'. Sebab mula2 aku ingatkan kita kena sort kan list tu dulu baru boleh cari nilai minumum dan maximum dia, rupanya2 tak sort pun takpe.

UNSORT:
# include <stdio.h>
# include <stdlib.h>

struct node
    {
    int data;
    struct node *link;
    };
struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    if(p==NULL)
        {
        p=(struct node *)malloc(sizeof(struct node));
        if(p==NULL)
            {
            printf("Error\n");
            exit(0);
            }
        p-> data = n;
        p-> link = NULL;
        }
    else
        p->link = insert(p->link,n);/* the while loop replaced by
                                    recursive call */
    return (p);
    };

void printlist ( struct node *p )
    {
    while (p!= NULL)
        {
        printf("%d --> ",p-> data);
        p = p-> link;
        }
    };

void main()
{
    int n;
    int x;
    struct node *start= NULL ;
    printf("Enter the # of nodes to be created \n");
    scanf("%d",&n);
    while ( n--> 0 )
        {
        printf( "Enter the data values to be placed in a node\n");
        scanf("%d",&x);
        start = insert ( start, x );
        printlist ( start );
        }
    printf("\n\nThe created list is\n");
    printlist ( start );
    printf("GND\n");
    struct node *r,*q;
    int min,max,i;
    r=start->link;
    q=r;
    min=r->data;
    r=r->link;
    while(r)
    {
        if(r->data<min)
            {
            min=r->data;
            r=start;
            }
        r=r->link;
    }
    r=start->link;
    q=r;
    max=q->data;
    while(r)
        {
            if(r->data>max)
            {
                max=r->data;
                q=r;
            }
        r=r->link;
        }
    printf("\nMinimum node values in the list: %d\n",min);
    printf("Maximum node values in the list: %d",max);
    printf("\n");
}


SORT:
# include <stdio.h>
# include <stdlib.h>

struct node
    {
    int data;
    struct node *link;
    };
struct node *insert(struct node *p, int n)
    {
    struct node *temp,*sem, *baru, *curr, *prev;
    if(p==NULL)
                {
                p = (struct node*)malloc(sizeof(struct node));
                p->data=n;
                p->link = NULL;
                }
             else
                {
                baru = (struct node*)malloc(sizeof(struct node));
                baru->data=n;
                baru->link = NULL;
                prev=NULL;
                curr=p;
                //searching stops when data is found or end of link.
                if(n<=curr->data)
                    {
                    baru->link=curr;
                    p=baru;
                    }
                else
                    {
                    while ((n>curr->data) && (curr->link != NULL))
                        {
                        prev=curr;
                        curr=curr->link;
                        };
                    if((n>curr->data) && (curr->link == NULL))
                            curr->link=baru;
                    else
                        {
                        prev->link=baru;
                        baru->link=curr;
                        }
                    }
                }
    return (p);
    };
void printlist ( struct node *p )
    {
    while (p!= NULL)
        {
        printf("%d --> ",p-> data);
        p = p-> link;
        }
    };
void main()
{
    int n;
    int x;
    struct node *start= NULL ;
    printf("Enter the # of nodes to be created \n");
    scanf("%d",&n);
    while ( n--> 0 )
        {
        printf( "Enter the data values to be placed in a node\n");
        scanf("%d",&x);
        start = insert ( start, x );
        printlist ( start );
        }
    printf("\n\nThe created list is\n");
    printlist ( start );
    printf("GND\n");
    struct node *r,*q;
    int min,max,i;
    r=start->link;
    q=r;
    min=r->data;
    r=r->link;
    while(r)
    {
        if(r->data<min)
            {
            min=r->data;
            r=start;
            }
        r=r->link;
    }
    r=start->link;
    q=r;
    max=q->data;
    while(r)
        {
            if(r->data>max)
            {
                max=r->data;
                q=r;
            }
        r=r->link;
        }
    printf("\nMinimum node values in the list: %d\n",min);
    printf("Maximum node values in the list: %d",max);
    printf("\n");
}

untuk soalan C:-
 c)  modify the program so as to reverse the linked list.

yang ni aku malas nak explain sebab aku ambik direct dari internet....

//NAME: MUHAMMAD AZRI BIN ABDUL KUDUS
//UiTM ID: 2009546345
# include <stdio.h>
# include <stdlib.h>

struct node
    {
    int data;
    struct node *link;
    };

struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    if(p==NULL)
        {
        p=(struct node *)malloc(sizeof(struct node));
        if(p==NULL)
            {
            printf("Error\n");
            exit(0);
            }
        p-> data = n;
        p-> link = p;
        }
    else
        {
        temp = p;
        while (temp-> link != p)
        temp = temp-> link;
        temp-> link = (struct node *)malloc(sizeof(struct node));
        if(temp -> link == NULL)
            {
                printf("Error\n");
                exit(0);
            }
        temp = temp-> link;
        temp-> data = n;
        temp-> link = p;
        }
        return (p);
}
struct node *reverse(struct node *p)
{
    struct node *prev, *curr;
        prev = NULL;
        curr = p;
        while (curr != NULL)
        {
            p = p-> link;
            curr-> link = prev;
            prev = curr;
            curr = p;
        }
        return(prev);
}

void printlist ( struct node *p )
{
        struct node *temp;
        temp = p;
        if(p!= NULL)
                {
                        printf("%d --> ",temp->data);
                        temp=temp->link;
                }while (temp!= p);
        else
        {
                printf("The list is empty\n");
        }
}

void main()
{
        int n;
        int x;
        struct node *start = NULL ;
        printf("Enter the number of nodes to create: \n");
        scanf("%d",&n);
        while ( n --> 0 )
        {
                printf( "Enter the data values to be placed in a node\n");
                scanf("%d",&x);
                start = insert ( start, x );
                printlist(start);
        }
        printf("\n\nDETAILS OF CREATED LIST:\n");
        printlist (start);
        printf("GND");
        start = reverse(start);
        printf("\n\nREVERSE LIST OF NUMBERS:\n");
        printlist(start);
        printf("GND");
}

1 comment:

Unknown said...

woow great coding..

Related Posts Plugin for WordPress, Blogger...