Saturday, May 18, 2013

C Source Codes for Implementing Linked List

The linked list can be implemented using the pointer structure in C so that we can

  • add an element
  • delete an element
  • search for an element
  • concatenate two linked list
  • Invert a linked list
  • Display elements in the list


//Program to demonstrate linked list operations

# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};

void main()
{
int a=111,b=2,c=3,will,wish,num;
struct node *ptr,*ptr2,*result,*temp;
void add(struct node **,int );
struct node * search(struct node *);
void display(struct node *);
void invert(struct node *);
void del(struct node *,int);
struct node * concat(struct node *,struct node *);
ptr=NULL;
ptr2=NULL;
result=NULL;        //result for storing the result of concatenation
////();
will=1;
printf("Main Menu\n1. Add element\n2.Delete element\n3.Search element\n4Linked List concatenation\n5.Invert linked list\n6. Display elements\n7.To exit\n\nPlease enter the choice");
while(will==1)
{

scanf("%d",&wish);
switch(wish)
{
case 1:
    printf("Enter the element you want to add  : ");
    scanf("%d",&num);
    add(&ptr,num);
    display(ptr);
    break;
case 2:
    printf("Enter the element to delete :");
    scanf("%d",&num);
    del(ptr,num);
    break;
case 3:
    printf("Now demonstrating search \n\t");
    temp = search(ptr);
    printf("Address of first occurence is  %u \t",temp);
    break;
case 4:
    /* Inputs given internally for demo only */
    printf(" Now demonstrating linked list concatenation\tPress any key to continue...");
    add(&ptr2,2);
    add(&ptr2,4);
    add(&ptr2,6);
    getch();
    printf("Displaying second Linked List");
    display(ptr2);
    getch();
    result = concat(ptr,ptr2);
    //();
    printf("Now Displaying the result of concatenation");
    display(result);
    getch();
    break;
case 5:

    printf("Inverting the list ...Press any key to continue...\n\t");
    invert(ptr);
    break;
case 6:
    display(ptr);
    break;
case 7:
     exit(1);
     break;
default:
    printf("Illegal choice");
}
printf("DO you want to continue ?   ");
scanf("%d",&will);
}    //end of while
}


void add(struct node **q,int num)
{
struct node *temp;
temp = *q;
if(*q==NULL)
{
    *q=malloc(sizeof(struct node));
    temp = *q;
}
else
{
    while((temp->link)!=NULL)
    {
        temp=temp->link;
    }
    temp->link = malloc(sizeof(struct node));
    temp=temp->link;
}
temp->data = num;
temp->link  = NULL;
}

void display(struct node *pt)
{

while(pt!=NULL)
{

    printf("\tData : %d\t",pt->data);
    printf("Link : %d\t",pt->link);
    pt=pt->link;
}
}


void invert(struct node *ptr)
{

struct node  *p,*q,*r;
p=ptr;
q=NULL;

while(p!=NULL)
{
    r=q;
    q=p;
    p=p->link;
    q->link=r;
}
ptr = q;
display(ptr);
}


//    CONCATENATION OF LINKED LISTS

struct node * concat(struct node *p,struct node *q)
{
struct node *x,*r;


if (p==NULL)
r=q;

if (q==NULL)
r=p;
else
{
      x=p;
      r=x;
      while(x->link!=NULL)
     x=x->link;
      x->link=q;
}
    return(r);
}


// SEARCHING AN ELEMENT IN THE LINKED LIST
// THIS FUNCTION FINDS THE FIRST OCCURENCE OF
// A DATA AND RETURNS A POINTER TO ITS ADDRESS

struct node * search(struct node *p)
{
struct node *temp;
int num;
temp = p;
printf("Enter the data that you want to search    ");
scanf("%d",&num);
printf("Link of temp %u", temp->link);
while(temp->link!=NULL)
    {
    printf("\nIn while\n ");
    if(temp->data == num)
    return(temp);
    temp=temp->link;
    }
return(NULL);
}



// DELETING DATA FROM THE LINKED LIST//

void del(struct node *p,int num)
{

struct node *temp,*x;
temp=p;
x= NULL;

while (temp->link !=NULL)
{
if(temp->data == num)
{
    if (x==NULL)
    {
    p = temp->link;
    free(temp);
    return;
    }
    else
    {
    x->link = temp->link;
    free(temp);
    return;
    }
}                  //end of outer if
x=temp;
temp=temp->link;
}    //end of while
printf("\n No such entry to delete \n");
}    //end of fn.


10 comments :

  1. There are lots of people who place bets on numerous staking pursuits, and sports betting is really the most Private company Toto site activity of several gamblers. Right now, sports wagering is a lot superior in demand. In case you check out this internet site, you'll receive an increasing number of specifics about the totosite.

    ReplyDelete
  2. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. slice invite code

    ReplyDelete
  3. With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money, slice invite code

    ReplyDelete
  4. buy twitch followers There's no denying that video games have become an integral part of the millennial life. This is probably because they allow the gamer to lead an alternate life, full of adventure and challenges. Gaming is a truly global industry today- a $60 billion one.

    ReplyDelete
  5. https://www.visualaidscentre.com/service/eyes-specialist-delhi/ There are many places that offer free online games for a person to fill their spare time with. There is a countless number of people who participate in online games each day, and many of those people would admit to how addictive these games can be.

    ReplyDelete
  6. https://www.buyyoutubesubscribers.in/2021/11/20/how-to-earn-money-from-youtube-views/ Making YouTube videos is a lot of fun. Nothing beats the feeling of accomplishment that you get when you see other people watching your video minutes after you uploaded it on YouTube. And to top it all off, you get that feeling of encouragement and motivation upon receiving positive feedback for your work.

    ReplyDelete
  7. https://dynamichealthstaff.com/jobs-in-dubai-for-nurses For a website to engage in SEO, it means that: 1. The company is considering to fund an internet marketing campaign, but probably unsure of what to do and how to do it; 2. The company has the option to hire a staff to do the SEO work (monthly salaries, health care, taxes, insurance included) or outsource the project to an SEO company who does the work for them, pay them for the services alone, and step out once the project is done.

    ReplyDelete
  8. https://hostinglelo.in YouTube has been a useful means of marketing goods for many years. If your aspiration is to reach a high number of viewers to your video on YouTube, make your video exciting enough to capture the interest of a diverse group of folks. In this article we will be offering helpful information to supplement your viewing audience on YouTube.

    ReplyDelete
  9. Best Places To Bet On Boxing - Mapyro
    Where To Bet On https://deccasino.com/review/merit-casino/ Boxing. It's a bsjeon.net sports betting ford fusion titanium event in which you bet on the outcome of a game. In the boxing world, each 출장샵 player gri-go.com must decide if or not to

    ReplyDelete
  10. You have given incredible substance abut the code implementation . I'm happy to find this post as I tracked down heaps of significant information in your article. tj maxx credit card payment online

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...