Lab 2-3
Lab 2-3
REPORT
Laboratory work nr. 2 - 3
at Computer Programming
A efectuat:
st. gr. FAF-171(L.engleza) Ion Vasilita
A verificat:
dr., conf.univ. Mihail Kulev
Chişinău 2018
Laboratory work no.2-3
Subject: Implementation of abstract data type of „ Simple Linked List” in C.
Objective: Obtaining the practical skills to implement an abstract data type(ADT) „Linked
List „ in C language.
Task: To write three files in C language for implementation and using ADT „Linked List”.
1. The header file list.h that contains: a) the structure specification of the simple shortlist
element; and b) the prototypes of the functions that provide the simple-to-do list
processing operations.
2. The list.cpp or list.c file that contains the codes (implementations) of all the declared
functions in the header file.
3. The lab2_3.cpp or lab2_3.c file that represents the program with the main () function for
processing the Simple Plain List with the display of the on-screen options menu, namely:
for lab work no. 2 - creating a simple list of n elements with dynamic elements, entering
the information about the keyboard list items, displaying the list element information on
the screen (as well as displaying the current and next element addresses), searching the
list item after an information field of the structure, the modification of the list element
fields, the interchange of 2 listed elements of the list, the determination of the length of
the list, the sorting of the list elements by a field, the determination of the last list
address, the release of the allocated dynamic memory for the list;
for laboratory work no. 3 - adding a new item at the end of the list, adding a new item
to the beginning of the list, inserting a new item in the list after the indicated element,
inserting a new item in the list before the indicated element, removing (deleting) from the
list, dividing the simple list in two separate lists, merging two lists in a list, writing
(saving) information about the list items to the file, reading information about the list
items in the file.
Some Notions
Linked lists are the best and simplest example of a dynamic data structure that uses pointers for
its implementation. However, understanding pointers is crucial to understanding how linked lists
work, so if you've skipped the pointers tutorial, you should go back and redo it. You must also be
familiar with dynamic memory allocation and structures.
A linked list is a set of dynamically allocated nodes, arranged in such a way that each node
contains one value and one pointer. The pointer always points to the next member of the list. If
the pointer is NULL, then it is the last node in the list.
A linked list is held using a local pointer variable which points to the first item of the list. If that
pointer is also NULL, then the list is considered to be empty.
File list.c
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FILE_NAME "tempFile.tmp"
#include"list.h"
int getListLength() {
int elements = 0;
printf("How many elements to generate: "); scanf("%d", &elements);
return elements;
}
Number * addToList(int Elements) {
srand(time(NULL));
Number *head = NULL;int i = 0;
for ( i = 0; i < Elements; i++) {
Number *newnumber = malloc(sizeof *newnumber);
newnumber->data = rand() % 9999;
newnumber->address = NULL;
if (!head)
head = newnumber;
else {
Number *iter = head;
for (; iter->address; iter = iter->address) {}
iter->address = newnumber;
}
}
printf("\nLinked list with %d elements was created", Elements);
return head;
}
void Menu() {
Number *head = NULL, *head1 = NULL, *head2 = NULL;
int userInput = 0, data = 0, data1 = 0;
MainMenu:
printf(
"------------ Menu ------------\n"
"1. Create list with n elements\n"
"2. Display information\n"
"3. Modify data\n"
"4. Swipe elements\n"
"5. Get last element address\n"
"6. Add number to last position\n"
"7. Add number to first position\n"
"8. Add number after indicated\n"
"9. Delete number from list\n"
"10. Cover the list\n"
"11. Join lists\n"
"12. Write list to file\n"
"13. Read list from file\n"
"14. Release the memory\n"
"15. Exit\n"
"Choice: "); scanf("%d", &userInput);
while (userInput == 0 || userInput > 15 || userInput < 0)
scanf("%d", &userInput);
switch (userInput) {
case 1:
head = addToList(getListLength());
_getch();
system("cls");
goto MainMenu;
break;
case 2:
system("cls");
printList(head);
_getch();
goto MainMenu;
break;
case 3:
printf("\nInput data to modify: "); scanf("%d", &data);
modifyInstance(head, data);
_getch();
system("cls");
goto MainMenu;
break;
case 4:
printf("\nInput source: "); scanf("%d", &data);
printf("Input destination: "); scanf("%d", &data1);
swipePosition(head, data, data1);
_getch();
system("cls");
goto MainMenu;
break;
case 5:
getLastElementAddress(head);
_getch();
system("cls");
goto MainMenu;
break;
case 6:
printf("\nInput element data: "); scanf("%d", &data);
addToLastPosition(head, data);
_getch();
system("cls");
goto MainMenu;
break;
case 7:
printf("\nInput element data: "); scanf("%d", &data);
head = addToFirstPosition(head, data);
_getch();
system("cls");
goto MainMenu;
break;
case 8:
printf("\nInput element after which to create: "); scanf("%d", &data);
printf("Input element data: "); scanf("%d", &data1);
insertOnSpecificPosition(head, data, data1);
_getch();
system("cls");
goto MainMenu;
break;
case 9:
printf("\nInput element data to remove: "); scanf("%d", &data);
deleteFromList(head, data);
_getch();
system("cls");
goto MainMenu;
break;
case 10:
splitList(head);
_getch();
system("cls");
goto MainMenu;
break;
case 11:
head1 = addToList(getListLength());
head2 = addToList(getListLength());
printList(head1);
printList(head2);
printList(joinLists(head1, head2));
_getch();
system("cls");
goto MainMenu;
break;
case 12:
writeListToFile(head, FILE_NAME);
_getch();
system("cls");
goto MainMenu;
break;
case 13:
readListFromFile(FILE_NAME);
_getch();
system("cls");
goto MainMenu;
break;
case 14:
if (head != NULL)
freeMemory(head);
_getch();
system("cls");
break;
default:
freeMemory(head);
break;
}
}
File main.c
void main(void) {
Menu();
system("cls");
printf("\n\n\n\n\n\t\t\t\tThank you!");
delay(1000);
system("cls");
exit(0);
}
Main Menu
Conclusion: In this laboratory work we obtaining the practical skills to implement an abstract
data type(ADT) „Linked List „ in C language.
Bibliography:
Sarcina de lucru pentru „Lucrarea de laborator 1 la SDA”. M.Kulev