-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertatmiddle.c
More file actions
37 lines (33 loc) · 833 Bytes
/
insertatmiddle.c
File metadata and controls
37 lines (33 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void traverse(struct node *ptr);
struct node * insertatbegin(struct node *head);
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *second = (struct node *)malloc(sizeof(struct node));
head->data = 5;
head-> next= second;
second->data = 6;
second->next = NULL;
traverse(head);
head = insertatbegin(head);
printf("\n");
traverse(head);
return 0;
}
void traverse(struct node *ptr){
while(ptr!=NULL){
printf("%d\t9 ",ptr->data);
ptr = ptr->next;
}
}
struct node * insertatbegin(struct node *head){
struct node *ptr = (struct node * )malloc(sizeof(struct node));
ptr->data = 4;
ptr->next= head;
return ptr;
}