C-PROGRAM LINKED LIST
#include<stdlib.h>
#include<stdio.h>
struct node
{
int a;
struct node *next;
};
void display(struct node *temp)
{
printf("\n elements in the list are\n");
while(temp!=NULL)
{
printf("\t %d",temp->a);
temp=temp->next;
}
}
struct node *insert(struct node *start)
{
struct node *temp,*prev;
int pos;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the number of elements");
scanf("%d",&temp->a);
printf("select the position to insert the number\n1.at the begning\n2.at the end\n3.at specified position");
scanf("%d",&pos);
switch(pos)
{
case 1:temp->next=start;
start=temp;
break;
case 2:prev=start;
while(prev->next!=NULL)
{
prev=prev->next;}
prev->next=temp;
temp->next=NULL;

break;
case 3:
printf("enter the position");
scanf("%d",&pos);
    C tutorial video for beginners

if(pos==1)
{
temp->next=start;
start=temp;
}
else if(pos>1)
{
prev=start;
pos--;
while(pos>1 && prev->next!=NULL)
{prev=prev->next;
pos--;}
temp->next=prev->next;
prev->next=temp;
}
else
{
printf("\n..............................invalid entry");
break;
}
default:printf("\n invalid entry");
}
return(start);
}

int main()
{
int c,ch;
struct node *start,*temp,*prev;
start=(struct node*) malloc(sizeof(struct node));
printf("enter the first number");
scanf("%d",&start->a);
start->next=NULL;
printf("do you want to continue");
scanf("%d",&c);
prev=start;
while(c==1)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("enter element");
scanf("%d",&temp->a);
prev->next=temp;
prev=temp;
temp->next=NULL;
printf("do you want to continue\n");
scanf("%d",&c);
}
while(1)
{
printf("enter your choice\n1.display\n2.insertion\n");
scanf("%d",&ch);
switch(ch)
{case 1:display(start);
break;
case 2:start=insert(start);
break;
case 3:exit(0);
}}return(0);
}

                                                         C Tutorial For beginners



FREE ZONE TUTOR.................................YOUR FEEDBACK IS OUR KEY TO SUCCESS..........FREE ZONE TUTOR.................................YOUR FEEDBACK IS OUR KEY TO SUCCESS...

Comments

Popular posts from this blog