TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Sort Linked List in Descending
Kaushik S
Nov 25
2015
Code
878
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef
struct
node{
int
data;
struct
node* next;
}node;
struct
node* head;
void
insert(
int
x)
{
struct
node* temp=NULL;
temp=(node*)malloc(
sizeof
(
struct
node));
temp->data=x;
if
(head==NULL)
{
temp->next=NULL;
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void
print()
{
struct
node* temp=head;
while
(temp !=NULL)
{
printf(
"%d"
,temp->data);
temp=temp->next;
}
}
void
sortAsc()
{
struct
node* temp=head;
struct
node* firstval=NULL;
int
val=0;
while
(temp!=NULL)
{
firstval=temp->next;
while
(firstval!=NULL)
{
if
(temp->data<firstval->data)
{
val=firstval->data;
firstval->data=temp->data;
temp->data=val;
}
firstval=firstval->next;
}
temp=temp->next;
}
printf(
" Sorted "
);
print();
}
void
main()
{
insert(2);
insert(4);
insert(9);
insert(8);
print();
sortAsc();
getch();
}
C
Datastructures
Linked List