-
Notifications
You must be signed in to change notification settings - Fork 2
/
insertBeg.c
48 lines (42 loc) · 842 Bytes
/
insertBeg.c
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
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *next;
};
typedef struct Node node;
node *head;
void insertBeg(int data) {
node *newNode;
newNode = (node*) malloc(sizeof(node));
newNode->data = data;
if(head == NULL) {
head = newNode;
head->next = head;
return;
}
node *currNode = head;
while(currNode->next != head) {
currNode = currNode->next;
}
currNode->next = newNode;
newNode -> next = head;
head = newNode;
}
void display() {
node *currNode;
currNode = head;
do {
printf("%d ", currNode->data);
currNode = currNode->next;
}while(currNode != head);
}
int main() {
insertBeg(6);
insertBeg(5);
insertBeg(4);
insertBeg(3);
insertBeg(2);
insertBeg(1);
display();
}