Data Structures And Data Organizations(CMPE242)

0 Yorum

Labwork 9

 

Laboratory Works

Laboratory work will be done at the laboratory.

 

Consider the following linked list node structure:

 

struct node{

            int data;

struct node *next;

};

 

Implement the following basic functions of the linear linked list structure:

 

  1. Check empty list

 

int is_empty( struct node *header)

{

            if(header==NULL)

                        return 1; //TRUE

else

return 0; //FALSE

}

 

  1. Create a node

 

struct node * newnode(int d)

{

            struct node *temp;

            temp=(struct node *) malloc(sizeof(node));

            temp->data =d;

            temp->next=NULL;

return temp;

}

 

 

  1. Insert a node into front/back/middle of LL

 

struct node * insertFront(struct node *header, int d)

{

            struct node *temp;

            temp=newnode(d);

            temp->next =header;

header=temp;

return header;

}

 

 

struct node * insertBack(struct node *header, int d)

{

            struct node *temp, *headertemp;

            temp=newnode(d);

if(header==NULL)

{

 

header=temp;

return header;

                        }

                        headertemp=header;

                        while(headertemp->next!=NULL)

headertemp =headertemp->next;

headertemp->next=temp;

                        return header;

}

 

void insertAfter(struct node *afterNode, int d)

{

            struct node *temp;

            temp=newnode(d);

                        temp->next=afterNode->next;

                        afterNode->next=temp;

}

 

  1. Delete a node from front/back/middle of LL

struct node * deleteFront(struct node *header)

{

            struct node *temp;

if(header==NULL)

            return header;

temp=header;

            header= header->next;

free(temp);

return header;

}

 

 

struct node * deleteBack(struct node *header)

{

            struct node *temp, *headertemp;

if(header==NULL)

 

return header;

                        headertemp=header;

                        while(headertemp->next->next!=NULL)

headertemp =headertemp->next;

                        temp=headertemp->next;

headertemp->next=NULL;

free(temp);

                        return header;

}

void deleteAfter(struct node *afterNode)

{

            struct node *temp;

            if(afterNode->next==NULL || afterNode==NULL)

                        return;

                        temp =afterNode->next;

                        afterNode->next=temp->next;

                        free(temp);  }

Task 1: Write down a complete C/C++ program to test your linear linked list implementation. Additionally, write another function which will be used to list the linked list content. Complete your implementation using the following code:

 

header = insertBack(header,2);

header = insertBack(header,4);

header = insertBack(header,6);

 DisplayList(header);

header = insertFront(header,1);

DisplayList(header);

insertAfter(header->next->next,5);

 DisplayList(header);

 header = deleteFront(header);

 DisplayList(header);

 header = deleteBack(header);

 DisplayList(header);

 deleteAfter(header->next);

 DisplayList(header);

 

Task 2: Write a function that moves a node forward in the given linked list. Take the pointer of the node to be moved as a parameter.

 

void moveforwardlist (struct node *, struct node *);

 

 

Task 3: Write a function that moves a node backward in the given linked list. Take the pointer of the node to be moved as a parameter.

 

void movebackwardlist (struct node *, struct node *);

 

Task 4: Write a function that loads the linked list with the given values of an array. Consider array and its size as input parameter to the function.

 

void loadlist (struct node *, int [ ], int );

 

 
 

 

Yorumlar (Toplam: 0)

Sıralama:
😭

Henüz yorum yapılmamış, ilk yorumu siz yazın.

Yorum Yaz

Onay Kodu .