Merge pull request #115 from Rupeshiya/master

sorting of linked list using selection sort
This commit is contained in:
Christian Bender 2018-03-04 15:14:30 +01:00 committed by GitHub
commit 2f1289ccab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 177 additions and 70 deletions

View File

@ -1,40 +1,98 @@
/* //program for stack using array
author: Christian Bender
This is the main-program for testing the stack.
*/
#include<stdio.h> #include<stdio.h>
#include <stdlib.h> void push();
void pop();
#include "stack.h" void peep();
void update();
int main() int main()
{ {
int n,a[100],top=0;
int MAX = 55; //function for pushing the element
int field[MAX]; void push()
int i; /* for the loop */
int *pInt = NULL;
initStack();
/* pushs some elements onto stack */
for (i = 0; i < MAX; i++)
{ {
field[i] = i; printf("\nenter the value to insert");
push((field + i)); // HERE scanf("%d",&n);
top=top+1;
a[top]=n;
} }
//function for poping the element out
/* pops the elements from the stack and print that out.*/ void pop()
for (i = 0; i < MAX; i++)
{ {
pInt = pop(); /* fetch next integer */ if(top==0)
{
printf("\nstack is empty");
}
else
{
int item;
item=a[top];
top=top-1;
printf("\npoped item is %d ",item);
}
}
//function for peeping the element from top of the stack
void peep()
{
int i;
printf("\nenter the element position to view from top");
scanf("%d",&i);
if(top-i+1<0)
{
printf("\nunderflow condition");
}
else
{
int x;
x=a[top-i+1];
printf("\nthe %dth element from top is %d",i,x);
}
}
//function to update the element of stack
printf("%d\n", *pInt); /* print value */ void update()
{
int i,n;
printf("\nenter the position to update");
scanf("%d",&i);
printf("\nenter the item to insert");
scanf("%d",&n);
if(top-i+1<0)
{
printf("\nunderflow condition");
}
else
{
a[top-i+1]=n;
}
}
int x;
while(1)
{
printf("\n1.push");
printf("\n2.pop");
printf("\n3.peep");
printf("\n4.update");
printf("\nenter your choice");
scanf("%d",&x);
switch(x)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
update();
break;
default:
printf("\ninvalid choice");
}
}
return(0);
} }
return EXIT_SUCCESS;
}

View File

@ -1,40 +1,89 @@
//sorting of linked list using selection sort
#include<stdio.h> #include<stdio.h>
struct node
{
int info;
struct node *link;
};
struct node *start=NULL;
//func to create node
struct node *createnode()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
return(p);
}
//program to insert at begining
void insert()
{struct node *t;
t=createnode();
printf("\nenter the value to insert");
scanf("%d",&t->info);
if(start==NULL)
{start=t;
}
else
{strutc node *p;
p=start;
t->link=p;
start=t;
}
//program to sort the linked list using selection sort
void sort()
{
struct node *p,*t;
t=start;
int tmp;
for(t=start;t->link!=NULL;t=t->link)
{
for(p=t->link;p!=NULL;p=p->link)
{
if(t->info>p->info)
tmp=t->info;
t->info=p->info;
p->info=tmp;
}
}
//program to view sorted list
void viewlist()
{
struct node *p;
if(start==NULL)
{
printf("\nlist is empty");
}
else
{
p=start;
while(p!=NULL)
{
printf("%d",p->info);
p=p->link;
}
}
int main() int main()
{ {
int array[100], n, i, j, position, swap; int n;
whhile(1)
printf("Enter number of elements\n"); {
printf("\n1.insert value at beg");
printf("\n2.sort the list");
printf("\n3.view value");
printf("\nenter your choice");
scanf("%d",&n); scanf("%d",&n);
switch(n)
printf("Enter %d integers\n", n); {case 1:
insert();
for ( i = 0 ; i < n ; i++ ) break;
scanf("%d", &array[i]); case 2:
sort();
for ( i = 0 ; i < ( n - 1 ) ; i++ ) break;
{ case 3:
position = i; viewlist();
break;
for ( j = i + 1 ; j < n ; j++ ) default:
{ printf("\ninvalid choice");
if ( array[position] > array[j] )
position = j;
}
if ( position != i )
{
swap = array[i];
array[i] = array[position];
array[position] = swap;
} }
} }
return(0);
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", array[i]);
return 0;
} }