forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_linked_list.c
110 lines (98 loc) · 1.96 KB
/
sorting_linked_list.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *nextptr;
}*new;
int main(void)
{
int n,an,sum;
printf("Enter the no of nodes you want ");
scanf("%i",&n);
createnode(n);
printf("Printing the unsorted nodes \n ");
displayList();
printf("Enter the no of nodes you want to add");
scanf("%i",&an);
sum = addnode(an,n);
printf("The sum of those nodes is %i ",sum);
}
int createnode(int n)
{
struct node *tmp,*fnnode;
int num,i;
new = (struct node *)malloc(sizeof(struct node));
printf("Input the data for 1 st node : ");
scanf("%i",&num);
new->data = num;
new->nextptr = NULL;
tmp = new;
for(i=2;i<=n;i++)
{
printf("Enter the data for the next node : ");
scanf("%i",&num);
fnnode = (struct node *)malloc(sizeof(struct node));
fnnode->data = num;
fnnode->nextptr = NULL;
tmp->nextptr = fnnode;
tmp = tmp->nextptr;
}
}
int displayList()
{
struct node *tmp;
tmp = new;
while(tmp!=NULL)
{
printf("Data is : %i \n ",tmp->data);
tmp = tmp->nextptr;
}
}
/*
int sot(int n)
{
struct node *tmp2 ,*sotnode;
int i,j,temp,k;
tmp2 = new;
if(tmp2 = NULL)
{
return;
}
else
{
for(i=0;i<n-1;i++,k--)
{
for(j = 1; j<k;j++)
{
if(tmp2->data > sotnode->data)
{
temp = tmp2->data;
tmp2->data = sotnode->data;
sotnode->data = temp;
}
tmp2 = tmp2->nextptr;
sotnode = sotnode->nextptr;
}
}
}
}
*/
int addnode(int an,int n)
{
struct node *tmp,*fsumnode,*tmp2;
int len = 0,i;
int sum = 0;
tmp = new;
while(len != an)
{
tmp = tmp->nextptr;
len++;
}
tmp2 = tmp;
for(i=an;i<=n;i++)
{
sum = tmp2->data + sum;
}
return sum;
}