//Program to print middle element of linked list without using integer variables.
#include<stdio.h>
#include<conio.h>
typedef struct node node;
struct node{
int data;
node *link;
};
node *start=NULL;
void insert()
{
node *ptr=(node*)malloc(sizeof(node));
int item;
printf("Enter data : ");
scanf("%d",&ptr->data);
ptr->link=start;
start=ptr;
}
void display()
{
node *ptr=start;
printf("\nList is : ");
while(ptr!=NULL)
{
printf("%d
",ptr->data);
ptr=ptr->link;
}
}
void print_middle()
{
node *ptr,*temp;
ptr=start;
temp=start->link;
while(temp->link!=NULL)
{
ptr=ptr->link;
temp=temp->link->link;
}
printf("\nMiddle element is : %d",ptr->data);
}
void main()
{
int n,i;
clrscr();
printf("\nEnter number of nodes in list : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
insert();
}
display();
print_middle();
getch();
}
Output:
nice
ReplyDeleteThanks :)
Delete