You are on page 1of 4

Data Structures and Algorithm Lab

Report

Submitted By:
Muhammad Mohsin
Reg#:
2017—EE—13

Submitted to:
Ma’am Sana Noor
Lab Report 5
Linked List

Main Program:
int main() { // Declaring the List object and its Pointer ...
List L;
Position P;
int i; /* Initializing the declared List object and pointing its pointer towards
the header of that List ... */
L = MakeEmpty(NULL);
P = Header(L);
int options=0;
int num;
while (options != 4){
printf("Your List: ");
PrintList(L);
puts("What would you like to do");
puts("1.Length of List");
puts("2.List Division");
puts("3.Insert");
puts("4.Exit");
printf("Your Choice: ");
scanf_s("%d", &options);
if (options == 1) {
printf("Element in the list = %d\n", ListLength(L));
getchar();
getchar();
system("cls");
}
else if (options == 2) {
ListDiv(L);
getchar();
getchar();
system("cls");
}
else if (options == 3) {
puts("Which number to insert");
scanf_s("%d", &num);
Insert(num, L, P);
P = Advance(P);
PrintList(L);
system("cls");

getchar();
return 0;
}
Lab Task 1:
List Length
Code:
int ListLength(List L) {
Position P = Header(L);
int i=0;
if (IsEmpty(L)){ /*First of all checks whether list is empty*/
puts("List Empty");
}
else {
do { /*Goes through the entire list*/
P = Advance(P);
i++;
} while (!IsLast(P, L));
}
return i;
}

Lab Task 2:
List Divide
Code:
void ListDiv(List L) {
Position P = Header(L); //Header of List
int length;
int pivot;

if (IsEmpty(L)){ /*First of all checks whether list is empty*/


puts("List Empty");
}
else {
length = ListLength(L);
pivot = length / 2;
int i;
List Small;/*Will contain elements smaller than the pivot*/
List Large;/*Will contain elements larger than the pivot*/
Small = MakeEmpty(NULL);
Large = MakeEmpty(NULL);
Position P_small = Header(Small); /*Header for the small list*/
Position P_large = Header(Large); /*Header for the small list*/
int pivot_element = 0;
for (i=1;i<length;i++){
P = Advance(P);
if (i == pivot) {
pivot_element = Retrieve(P);
printf("Pivot element is %d\n", pivot_element);
break;
}

}
P = Header(L);
do {
P = Advance(P);
if (Retrieve(P)<pivot_element) {
Insert(P->Element, Small, P_small);
P_small = Advance(P_small);
}
else if (Retrieve(P) > pivot_element) {
Insert(P->Element, Large, P_large);
P_large = Advance(P_large);
}
}while (!IsLast(P, L));
printf("Smaller List:");
PrintList(Small);
printf("Larger List:");
PrintList(Large);
}
}

You might also like