Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to create a singly linked list of numbers using threads

Posted By: Easy Tutor     Category: C Programming     Views: 3116

Write a program to create a singly linked list of numbers using threads.

Code for Program to create a singly linked list of numbers using threads in C Programming

# include <stdio.h>
# include <pthread.h>

struct parastruct
{
    int from;
    int to;
}*para;


struct node
{
    int no;
    struct node *next;
};

void *createlist_thread(void *parast);

int main()
{
    int iNumber;
    int iCount;
    struct node *list1,*list2,*head;
    pthread_t tid;
    printf("Enter Number up to Which you want to Create List :");
    scanf("%d",&iNumber);

    if(iNumber<=1)
    {
        printf("\nError : Enter Number Greater than 1 \n\n");
    }
    else
    {
        para=(struct parastruct*)malloc(sizeof(struct parastruct));
        para->from=(iNumber/2) + 1;
        para->to=iNumber;
        pthread_create(&tid,NULL,createlist_thread,(void *)para);
        
        for(iCount=1;iCount<=iNumber/2;iCount++)
        {
            if(iCount==1)
            {
                list1=(struct node*)malloc(sizeof(struct node));
                head=list1;
            }
            else
            {
                list1->next=(struct node*)malloc(sizeof(struct node));
                list1=list1->next;
            }
            list1->no=iCount;
            list1->next=NULL;
        }
        
        pthread_join(tid,(void**)&list2);
        
        
        list1->next=(struct node*)malloc(sizeof(struct node));
        list1->next=list2;

        list1=head;
        printf("\nList Added by Main ...\n");
        for(iCount=1;iCount<=iNumber;iCount++)
        {            
            if(iCount==para->from)
            {
                printf("\nList Added by Thread ...\n");                            }
            printf("%d \t",list1->no);
            list1=list1->next;
        }
    }        
    printf("\n");
    return 0;
}

void *createlist_thread(void *para)
{
    struct parastruct *paralist;
    struct node *list2;
    struct node *head;
    int iCount;
    paralist = (struct parastruct*) para;
    for(iCount=paralist->from;iCount<=paralist->to;iCount++)
    {
        if(iCount==paralist->from)
        {
            list2=(struct node *)malloc(sizeof(struct node));
            head=list2;
        }
        else
        {
            list2->next=(struct node *)malloc(sizeof(struct node));
            list2=list2->next;
        }    
        list2->no=iCount;
        list2->next=NULL;
    }
    list2=head;
    pthread_exit((void*)list2);
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to create a singly linked list of numbers using threads is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!