What's new

C & C++ Pa tulong po ulit C programming

Ano po mali sa source code ko at hindi masort at maalis yung duplicate number? Ano po dapat baguhin, salamat in advanced.


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

//structure of a node
struct node
{
    int data;                //data
    struct node *next;        //address
    
} *head;

void createList(int n);
void insertNodeAtBeginning(int data);
void reverseList();       
void sortList();           
void removeDuplicateList();       
void displayList();

int main()
{
    int n, data, i, choice;
    int arr[i];
    int temp = 0;

    int j, k;
    int size;
    
    //calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[i]);
    
    //create a linked list of n nodes
    printf("Enter the total number of nodes: ");
    scanf("%d", &n);
    createList(n);
    
    printf("\nData in list \n");
    displayList();
    
    //insert data at the beginning of the linked list
    printf("\nEnter data to insert at the beginning of the list: ");
    scanf("%d", &data);
    insertNodeAtBeginning(data);
    
    printf("\nData on the list\n");
    displayList();
    
    //reverse the list
    printf("\nPress 0 to reverse the order of the list: ");
    scanf("%d", &choice);
    if(choice == 0)
    {
        reverseList();
    }
    
    printf("\nData in the list\n");
    displayList();
    
    //diplaying elements of array after sorting
    printf("\nPress 1 to sort the array in ascending order: ");
    scanf("%d", &data);
    for(int i = 0; i < length; i++)
    {
        sortList(data);
    }

    printf("\nData in the list\n");
    displayList();
    
    //display an array after removing the duplicate elements
    printf("\nArray elements after removing the duplicate elements\n");       
    removeDuplicateList();

    printf("\nData in the list\n");
    displayList();   
    
    return 0;
}


//create a list of nodes
void createList(int n)
{
    struct node *newNode, *temp;
    int data, i;
    
    if(n <= 0)
    {
        printf("List size must be greater than zero\n");
        return;
    }

    head = (struct node *)malloc(sizeof(struct node));
    
    //if unable to allocate memory for head node
    if (head == NULL)
    {
        printf("UNABLE TO ALLOCATE MOMORY");
    }
    else
    {
        //input data of node from the user
        printf("Enter the data of node 1: ");
        scanf("%d", &data);
        
        head->data = data;        //linked data field with data
        head->next = NULL;        //linked address field to NULL
        
        temp = head;
        
        //create n nodes and adds to linked list
        for(i=2; i<=n; i++)
        {
            newNode = (struct node *)malloc(sizeof(struct node));
            
            //if memory is not allocated for newNode
            if(newNode == NULL)
            {
                printf("UNABLE TO ALLOCATE MEMORY");
                break;
            }
            else
            {
                printf("Enter the data of node %d: ", i);
                scanf("%d", &data);
                
                newNode->data = data;        //link data field of newNode with data
                newNode->next = NULL;        //link address field of newNode with NULL
                
                temp->next = newNode;        //temp to the newNode
                
                temp = temp->next;   
            }
        }
        printf("LIST CREATED SUCCESSFULLY\n");
    }
    
    
}

//create a new node and inserts at the beginning of the linked list
void insertNodeAtBeginning(int data)
{
    struct node *newNode;
    
        newNode = (struct node *)malloc(sizeof(struct node));
        
        if(newNode == NULL)
        {
            printf("UNABLE TO ALLOCATE MEMORY");
        }
        else
        {
            newNode->data = data;        //linked data part
            newNode->next = head;        //linked address part
            
            head = newNode;                //make newNode as first node
            
            printf("DATA INSERTED SUCCESSFULLY\n");   
        }   
}

//reverse the list
void reverseList()
{
    struct node *prevNode, *curNode;

    if(head != NULL)
    {
        prevNode = head;
        curNode = head->next;
        head = head->next;
        
        // Make first node as last node
        prevNode->next = NULL;

        while(head != NULL)
        {
            head = head->next;
            curNode->next = prevNode;

            prevNode = curNode;
            curNode = head;
        }
        
        // Make last node as head
        head = prevNode;

        printf("SUCCESSFULLY REVERSED LIST\n");
    }
}


//sorting of the linked list
void sortList(int data)                 
{
    int i, j, k;               
    int arr[i];
    int temp = 0;
    
    //calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[i]);

    for(int i=0; i<length-1; i++)           
    {
        for(int j = 0; j<length-i-1; j++)
        {
            if(arr[j] >= arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;   
            }
        }
            printf("DATA SORTED SUCCESSFULLY\n");
    }
    
        for(int i=0; i<length; i++){
            printf("%d", arr[i]);
        }
    
                    
}



//removing the duplicate in the list
void removeDuplicateList()               
{
    
    int i, j, k;
    int size;
    int arr[i];
    
    //calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[i]);
    
//use nested for loop to find the duplicate element
for (i= 0; i<size; i++)                       
{
    printf("%d", arr[i]);
}

for (i= 0; i<size; i++)                       
{
    for(j=i+1; j<size; j++ )
    {
        //use if statement to check duplicate element
        if(arr[i] == arr[j])
        {
            //delete the current position of the duplicate element
            for(k=j; k<size-1; k++)
            {
                arr[k] = arr[k+1];
            }
            //decrease the size of array after removing the duplicate element
            size--;
            
            //if the position of the elements changes, don't decrease the indes j
            j--;
        }
    }
}

printf("SUCCESSFULLY REMOVED THE DUPLICATE DATA\n");   
    
    
}


//display entire list
void displayList()
{
    struct node *temp;
    int n;
    
    if(head == NULL)
    {
        printf("LIST IS EMPTY");
    }
    else
    {
        temp = head;
        while(temp != NULL)
        {
            printf("Data = %d\n", temp->data);        //print data of current node
            temp = temp->next;                        //move to next node
        }
    }

}
 

Similar threads

Back
Top