SAVITIBAI PHULE UNIVERSITY OF PUNE
S. Y. B.Sc. (Computer Science) Semester III
Practical Examination
SUBJECT: CS-233 Practical course based on CS231
/* Q2 Write a program that sorts the elements of linked list using bubble sort technique */
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *Next;
}*Head, *Last;
void CreateList()
{
struct Node *NewNode = (struct Node*) malloc(sizeof(struct Node));
if (NewNode != NULL)
{
printf("Enter the data value: ");
scanf("%d", &(NewNode->data));
NewNode->Next = NULL;
if (Head == NULL)
{
Head = Last = NewNode;
}
else
{
Last->Next = NewNode;
Last = NewNode;
}
}
}
void Display()
{
struct Node *temp = Head;
printf("\n\nThe given Linked List is : ");
while (temp != NULL)
{
if (temp->Next != NULL)
printf("%d ->", temp->data);
else
printf("%d", temp->data);
temp = temp->Next;
}
}
void SortLinkedList()
{
struct Node *current = Head, *index = NULL;
int temp;
if (Head != NULL)
{
while (current != NULL)
{
index = current->Next;
while (index != NULL)
{
if (current->data > index->data)
{
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->Next;
}
current = current->Next;
}
}
}
int main()
{
int pos = 0, choice = 0;
do
{
printf("\n\nEnter your choice: \n1. Create \n2. Display \n3. Sort the List \n4. Exit \n\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
CreateList();
break;
case 2:
Display();
break;
case 3:
SortLinkedList();
break;
default:
printf("Thank you visit again ....\n\n");
break;
}
}while(choice != 4);
system("pause");
return 0;
}
No comments:
Post a Comment