SINGLY LINKED LIST USING C
- Get link
- X
- Other Apps
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void display(struct node *temp)
{
printf("\nElements in the list are===");
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
struct node * insert(struct node *start)
{
struct node *temp,*prev;
int pos;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter new");
scanf("%d",&temp->data);
printf("select the position \n1-At begining\n2--At end\n3---At specific 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);
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("invalid entry");
break;
default:
printf("Invalid entry");
} return(start);
}
void main()
{
int choice;
struct node *start,*prev,*temp;
start=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the First element:::");
scanf("%d",&start->data);
start->next=NULL;
prev=start;
printf("\npress 1 to enter more or 0 for exit::::::::");
scanf("%d",&choice);
while(choice==1)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the next");
scanf("%d",&temp->data);
temp->next=NULL;
prev->next=temp;
prev=temp;
printf("\npress 1 to enter more or 0 for exit::::");
scanf("%d",&choice);
}
while(1)
{
printf("\nList created[select an operation]\n");
printf("\n1-Display\n2--Insert\n3---Exit:::");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(start);
break;
case 2:
start=insert(start);
break;
case 3:
exit(0);
break;
default:
printf("invalid input"); break;
}
}
}
- Get link
- X
- Other Apps
Popular posts from this blog
FIND NUMBER OF VOWELS IN A STRING
FIND NUMBER OF VOWELS IN A STRING c=raw_input("Enter A String:") b=c.lower() x=0 for letter in b: if letter in "aeiou": x+=1 print "Total Number Of Vowels = " + str(x) print "Number Of Vowels In Each" d,e,f,g,h=0,0,0,0,0 for letter in b: if letter in "a": d+=1 print "a=" +str(d) for letter in b: if letter in "e": e+=1 print "e=" +str(e) for letter in b: if letter in "i": f+=1 print "i=" +str(f) for letter in b: if letter in "o": g+=1 print "o=" +str(g) for letter in b: if letter in "u": h+=1 print "u=" +str(h) OUTPUT
TO FIND EVEN NUMBERS UP TO N
ENTERED NUMBER IS PRIME OR NOT
ENTERED NUMBER IS PRIME OR NOT n=int(input("enter a number:")) if n<0: print "enter a valid input" elif n<2: print "neither prime nor composite" else: for i in range(2,n/2): if(n%i)==0: print n,"is not a prime number" break else: print n,"is a prime number" OUTPUT --------------------------------------------------------------------------------------------------------------------------

Comments
Post a Comment