Sorted linked list C#

Create and Sort a Single Linked List - C Program

  • Home > C Programs > C Linked List programs
  • « Previous
  • Next »
    Programs
  • C Linked List Programs
  • Create & Sort a single linked list
  • Create Odd & Even linked lists
  • Insert node in doubly linked list
  • Delete user specified node from circular linked list
  • Search an element in linked list
  • Print linked list in reverse order
Write a C program to create a single linked list and then sort that list using separate function [Use local pointer i.e. with return statement].

Solution:

Linked List:
Linked list is a linear data structure and each element containing the address of its successor. A linked list represents by a pointer to the first node of the linked list. The first node is called head.

In this example we developed two functions one for insertion of linked list and another is for sorting of elements.

Program

#include
#include
#include
typedef struct node
{
int info;
struct node *next;
}nodetype;
nodetype* insert[nodetype*];
void sort[nodetype*];
void main[]
{
nodetype* left=NULL;
nodetype* right=NULL;
int ch,c;
printf["Enter 1 for insert \nEnter 2 for sort \nEnter 3 for exit"];
do
{
printf["\nEnter choice: "];
scanf["%d",&ch];
switch[ch]
{
case 1:
right=insert[right];
if[left==NULL]
{
left=right;
}
break;
case 2:
sort[left];
case 3:
exit[1];
default:
printf["invalid choice"];
break;
}
printf["do you want to continuoe press 1: "];
scanf["%d",&c];
}while[c==1];
getch[];
}
nodetype* insert[nodetype *r]
{
i.nt n;
nodetype *p;
p=[nodetype*]malloc[sizeof[nodetype]];
printf["\nEnter the number: "];
scanf["%d",&n];
if[p!=NULL]
{
p->info=n;
p->next=NULL;
if[r==NULL]
{
r=p;
}
else
{
r->next=p;
r=p;
}
return[r];
}
else
{
printf["not enough memory"];
}
}
void sort[nodetype *l]
{
nodetype *t;
nodetype *s;
int x;
t=l;
while[t!=NULL]
{
s=t->next;
while[s!=NULL]
{
if[t->info>s->info]
{
x=t->info;
t->info=s->info;
s->info=x;
}
s=s->next;
}
t=t->next;
}
t=l;
while[t!=NULL]
{
printf[" %d ",t->info];
t=t->next;
}
}


Output:

  • « Previous
  • Next »

Video liên quan

Chủ Đề