Skip to main content

Singly Linked List

/**
    Program Name: Singly Linked List

    Description: This Program is for Implemeting Singly Linked List

    Author:  Tauqirul Haque
       
*/


struct Linklist
{
    int item;
    struct Linklist *next;
};

typedef struct Linklist node;

void insertAtBeginning(node **);
void createAppendNode(node **);
void insertInMiddle(node **);
void deleteNode(node **);
void displayNode(node **);
void countNode(node **); 
void searchElement(node **);
void reverse(node **);


void main()
{
    node *head = NULL;
    int choice = 0;
    while(choice != 9)
    {
       
    printf("\n\t\t\t1. Add Node At The Beginning ");
    printf("\n\t\t\t2. Insert Element in the Middle ");
    printf("\n\t\t\t3. Append New Node ");
    printf("\n\t\t\t4. Delete Node ");
    printf("\n\t\t\t5. Display Node ");
    printf("\n\t\t\t6. Count Nodes ");
    printf("\n\t\t\t7. Search Element ");
    printf("\n\t\t\t8. Reverse The List");
    printf("\n\t\t\t9. Exit Program ");
    printf("\n Enter Your Choice #  ");
   
    scanf("%d",&choice);
   
    switch(choice)
    {
        case 1:
                  insertAtBeginning(&head);
                  break;
         case 2:
                 insertInMiddle(&head);
                 break;
        case 3:
                createAppendNode(&head);
                break;               
        case 4:
                deleteNode(&head);
                break;
        case 5:
                displayNode(&head);
                break;
        case 6:
                countNode(&head);
                break;
        case 7:
                searchElement(&head);
                break;
        case 8:
                reverse(&head);
                break;
               
        case 9:
                printf("\n\nProgram Terminating ... \n");
                exit(0);
    }
    }
}

void createAppendNode(node **head)
{
    int data;
    printf("Enter The Data  #  ");
    scanf("%d",&data);
   
    node *temp, *newNode;  //temp is just for temporarily getting head address and newNode for inserting new node
   
    temp = *head;  //storing head's address
   
    if(*head == NULL)    //if no node then;
    {
        temp = (node *)malloc(sizeof(node));  //allocating memory for temp
        temp->item = data;
        temp->next = NULL;
        *head = temp;   //now storing the value of temp in head,, head is the first node
    }
    else
    {
        temp = *head;
        while(temp->next != NULL)   //moving to the last node
        {
            temp = temp->next;
        }
       
        newNode = (node *)malloc(sizeof(node ));  //allocating memory for the new node
       
        newNode->item = data;  //assigning value to newNode
        newNode->next = NULL; //assigning  NULL to the address of newNode
       
        temp->next = newNode;   //now the old list points to the newly created node;
    }
}

void displayNode(node **head)
{

    node *temp = *head;

    if(temp == NULL)    //checking if the list is empty then printing No elements to display
    {
        printf("\n\nThe List is EMPTY!!!\n\n");
        return;
    }

    printf("\n\nThe Elements of the List are #  ");

    while(temp != NULL)        //running the loop till the end to display every element
    {
        printf("%4d",temp->item);
        temp = temp->next;
    }
    printf("\n\n");
}


void deleteNode(node **head)
{
    node *temp, *previousNode; //previousNode will hold the address of previous node
    int data;
    int flag = 0; //assuming the element is not in the list
   
    temp = *head;
   
    if(temp == NULL)
    {
        printf("\n\n List EMPTY!!! .. No Element To Be Deleted \n\n");
        return;
    }
    printf("\n\nEnter The Element To Be Deleted  #  ");
    scanf("%d",&data);
   
    while(temp != NULL)
    {
        if(temp->item == data)
        {
            if(temp == *head)  //checking for the first node
            {
                *head = temp->next;    //shifting the head address to the next node
            }
            else
            {
                previousNode->next = temp->next;  //storing the address of previous node
            }
           
            flag = 1;  //item found
           
            free(temp);    //releasing the memory
        }
        else
        {
            previousNode = temp;
            temp = temp->next;
        }
    }
       
        if(flag==1)
        {
            printf("\n\nThe Element %d was Successfully DELETED \n\n",data);
        }
        else
        {
            printf("\n\nThe Element %d is NOT in The List \n\n",data);
        }   
}


void countNode(node **head)
{
    int count = 0;
    node *temp = *head;
   
    if(temp == NULL)
    {
        printf("\n\nList Empty ... No Elements\n\n");
    }
    else
    {
        while(temp != NULL)
        {
            count++;
            temp = temp->next;
        }
       
        printf("\n\nTotal Nodes #  %d ",count);
    }
}


void insertAtBeginning(node **head)
{
    node *temp;    //this will be our new node
    int data;
   
    printf("\n\nEnter The Element You Want To Insert  #  ");
    scanf("%d",&data);
   
    temp = (node *)malloc(sizeof(node));
   
    if(temp == NULL)
    {
        printf("\n\n No Memory Available .... OVERFLOW \n\n");
        return;
    }
   
    temp->item = data ; //assigning the element
    temp->next = *head; //now this will point to the head node as this will be the first element
   
    *head = temp;  //now making head point to this node
}

void insertInMiddle(node **head)
{
    node *newNode;
    node *temp = *head;
    int data;
    int count = 1;
    int flag = 0; 
    int location;
   
    if(temp == NULL)
    {
        printf("\n\nList Empty ... Cannot Insert \n\n");
        return;
    }
   
    printf("\n\nEnter The Location Where You Want to Insert New Node  # ");
    scanf("%d",&location);
    printf("\nEnter The Element You Want To Insert #  ");
    scanf("%d",&data);
   
    newNode = (node *)malloc(sizeof(node));
   
    if(newNode == NULL)
    {
        printf("\n\nMemory Full...OVERFLOW !!! \n\n");
        return;
    }
   
    while(temp != NULL)
    {
        if(count == location)
        {
            newNode->item = data;
            newNode->next = temp->next;
           
            temp->next = newNode;
            flag = 1;
        }
       
        temp = temp->next;
        count++;
    }
   
    if(flag == 1)
    {
        printf("\n\nNode Inserted Successfully ... \n\n");
    }
    else
    {
        printf("\n\nSpecified Location Does Not Exits ..\n\n");
    }
}

void searchElement(node **head)
{
    node *temp = *head;
    int item;
    int location = 1;
   
    printf("\n\nEnter The Element To Be Searched #  ");
    scanf("%d",&item);
   
    while(temp != NULL)
    {
        if(temp->item == item)
        {
            printf("\n\nSearch Successfull.. Element Found At Location %d \n\n", location);
            return;
        }
       
        location++;
       
        temp = temp->next;
    }
   
    printf("\n\nSearch UNSUCCESSFULL !!!... Element Not In The List\n\n");
}     

void reverse(node **head)
{
    node *tempHead = *head;
    node *reverseNode = NULL;
    node *swapper;
   
    while(tempHead)   //this loops runs while the value of tempHead is not NULL
    {
        swapper = reverseNode;  //doing the swapping job
        reverseNode = tempHead;
        tempHead = tempHead->next;
        reverseNode->next = swapper;
    }
   
    *head = reverseNode;  //now making the head point to the reversed list
}
       
       
   

Comments

Anonymous said…
buy tramadol online buy tramadol online with a cod - cheap tramadol usa
Anonymous said…
buy xanax no prescription best place order xanax online - buy xanax online express shipping
Anonymous said…
xanax online xanax drug wikipedia - xanax xr generic price
Anonymous said…
tramadol generic tramadol 50 mg veterinary use - tramadol withdrawal leg cramps
Anonymous said…
order xanax online xanax drug tests detection periods - generic xanax round blue pill
Anonymous said…
buy tramadol online cheap tramadol with no prescription - buy tramadol from thailand
Anonymous said…
carisoprodol 350 mg carisoprodol common side effects - other names carisoprodol 350 mg
Anonymous said…
buy tramadol online cheap-tramadol.org - reliable online pharmacy tramadol
Anonymous said…
buy tramadol online tramadol no prescription cod - buy tramadol hcl
Anonymous said…
buy cialis online can i buy cialis over the counter in usa - cialis stories
Anonymous said…
xanax xanax 2mg bars generic - alprazolam er 0.5 mg tablet
Anonymous said…
buy cialis online cialis price list - cialis online aus holland
Anonymous said…
buy xanax online effects of .75 mg xanax - xanax effects unborn child
Anonymous said…
buy cialis online buy cialis online nz - buy generic cialis in usa
Anonymous said…
cialis 10mg buy cialis online fast shipping generic - cialis 2.5 mg price
Anonymous said…
buy tramadol free shipping tramadol 50 mg time release - tramadol 50mg recreational
Anonymous said…
http://buytramadolonlinecool.com/#51726 tramadol hcl 50 mg overdose - buy tramadol for dogs usa
Anonymous said…
buy tramadol for dogs information on tramadol 50mg tablets - buy tramadol online usa
Anonymous said…
buy tramadol online order tramadol pharmacy - lethal dosage tramadol
Anonymous said…
buy tramadol overnight tramadol addiction help - tramadol for dogs used by humans
Anonymous said…
learn how to buy tramdadol how to buy tramadol online overnight - tramadol hcl 50mg 627
Anonymous said…
lorazepam mg narcan for ativan overdose - 2mg ativan high
Anonymous said…
buy tramadol online tramadol 50mg better than vicodin - tramadol 100mg usa

Popular posts from this blog

Splitting Large File into Smallers ones

Splitting of Large File in Linux/Unix is done using split command. Check the example given below Using split command on a 600MB myImage.iso file. - prompt# split -b 200m image.iso image this command will generate three files, namely imageaa, imageab, imageac, of 200MB each. afterwards we can use the cat command to combine the three to get back the original file, the command goes as follows ... - prompt# cat imagea* > image. iso . Things Done the easy ways ...

Extracting RPM

Sometimes we are required to extract files inside an RPM file without installing it. For example is when we take binaries from one distribution and use it on another distribution, where RPM is not the default package manager. The rpm2cpio command comes in handy. Check out the given example below ... $ rpm2cpio coreutils-6.9-2.fc7.i586.rpm | cpio -idv ./bin/basename ./bin/cat ./bin/chgrp .... ... [.. etc ] Now you can use the extracted files

Getting Linux CD Free to Your Door-Steps

Getting a UBUNTU Linux CD at your doorstep is very easy, and this all comes to you at NO-COST.. no need to pay a single cent. All you need to do is to follows these given steps .... Step 1: Log on to https:/ /shipit. ubuntu. com Step 2: Click on the Required CD you want .. (i.e. Desktop or Server Version) Step 3: Log into your account or create a new one ( if you don't have one) Step 4: Fill in your details with proper Address and done .... The CD will arrive you with 1-1.5 month ... ........... Happy Linux .............