Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct Node{
- int data;
- struct Node *next;
- }*head;
- void createlist(int n);
- void display();
- void middle();
- int main()
- {
- int n;
- printf("Enter the number of nodes: ");
- scanf("%d",&n);
- createlist(n);
- printf("Data Entered in Linked list are:\n");
- display();
- middle();
- return 0;
- }
- void createlist(int n)
- {
- struct Node *p, *temp;
- int data,i;
- head= (struct Node*)malloc(sizeof(struct Node));
- printf("Enter data for node 1: ");
- scanf("%d",&data);
- head->data=data;
- head->next=NULL;
- p=head;
- temp=head;
- for(i=2;i<=n;i++)
- {
- printf("Enter data for node %d: ",i);
- scanf("%d",&data);
- temp=(struct Node*)malloc(sizeof(struct Node));
- temp->data=data;
- temp->next=NULL;
- p->next=temp;
- p=p->next;
- }
- }
- void display()
- {
- struct Node* temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
- void middle()
- {
- struct Node *p , *q;
- p=head;
- q=head;
- while( q && q->next)
- {
- p=p->next;
- q=q->next->next;
- }
- printf("Middle Element is : %d",p->data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement