0% found this document useful (0 votes)
34 views13 pages

Lab 2-3

This document describes a laboratory work assignment to implement an abstract data type (ADT) of a simple linked list in C language. The tasks are to create 3 files: 1) a header file containing the list element structure and function prototypes, 2) an implementation file containing the function codes, and 3) a program file containing a main function to process the linked list with menu options such as creating the list, adding/deleting elements, sorting, and saving to a file.

Uploaded by

Istrate Nicolae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views13 pages

Lab 2-3

This document describes a laboratory work assignment to implement an abstract data type (ADT) of a simple linked list in C language. The tasks are to create 3 files: 1) a header file containing the list element structure and function prototypes, 2) an implementation file containing the function codes, and 3) a program file containing a main function to process the linked list with menu options such as creating the list, adding/deleting elements, sorting, and saving to a file.

Uploaded by

Istrate Nicolae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Ministerul Educaţiei al Republicii Moldova

Universitatea Tehnică a Moldovei

Departamentul Ingenerie Softwaer si Automatica

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.

Let's define a linked list node:


typedef struct node {
int val;
struct node * next;
} node_t;
Description of file contents and program code in C:
File list.h
typedef struct Numbers {
int data;
struct Number *address;
} Number;

/* Methods which return struct Numbers */


Number* addToFirstPosition(Number *, int);
Number* addToLastPosition(Number *, int);
Number* addToList(int);
Number* deleteFromList(Number *, int);
Number* insertOnSpecificPosition(Number *, int, int);
Number* joinLists(Number *, Number *);
Number* modifyInstance(Number *, int);
Number* swipePosition(Number *, int, int);

/* Methods which reuturn integers */


int getListLength();

/* Methods with empty return */


void delay(int);
void freeMemory(Number *);
void getLastElementAddress(Number *);
void Menu();
void printList(Number *);
void readListFromFile(char*);
void splitList(Number *);
void writeListToFile(Number *, char*);

File list.c

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FILE_NAME "tempFile.tmp"
#include"list.h"

void delay(int mseconds)


{
clock_t goal = mseconds + clock();
while (goal > clock());
}

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;
}

Number * modifyInstance(Number *head, int data) {


int dataToModify = 0; Number *iter = head;
printf("Value to change with: "); scanf("%d", &dataToModify);
for (; iter; iter = iter->address)
if (iter->data == data)
iter->data = dataToModify;
printf("\nElement: %d was modified successfully", data);
return head;
}

Number * swipePosition(Number *head, int source, int destination) {


Number *iter = head;
if (source != destination) {
for (; iter; iter = iter->address) {
if (iter->data == source) {
iter->data = destination;
}
else if (iter->data == destination) {
iter->data = source;
}
}
}
else
printf("Source data correspond with destination data, no need to swipe");
printf("\nElement: %d was swiped with %d", source, destination);
return head;
}

Number * addToLastPosition(Number *head, int data) {


if (head == NULL)
return NULL;
Number *iter = head;
while (iter->address != NULL)
iter = iter->address;
Number *newNumber = NULL;
newNumber = malloc(sizeof(newNumber));
newNumber->data = data;
newNumber->address = NULL;
iter->address = newNumber;
printf("\nElement: %d was inserted to last position",data);
return head;
}

Number * addToFirstPosition(Number *head, int data) {


Number *newNumber = NULL;
newNumber = malloc(sizeof(newNumber));
newNumber->data = data;
newNumber->address = head;
head = newNumber;
printf("\nElement: %d was inserted on first position",data);
return head;
}

Number *insertOnSpecificPosition(Number *head, int elementBefore, int data) {


Number *newNumber = NULL;Number *iter = head;
newNumber = malloc(sizeof(newNumber));
newNumber->data = data;
for (; iter; iter = iter->address)
if (iter->data == elementBefore) {
newNumber->address = iter->address;
iter->address = newNumber;
}
printf("\nElement: %d was inserted successfully", data);
return head;
}

Number *deleteFromList(Number *head, int dataToRemove) {


Number *newElement = head; Number *iter = head;
int nextAddress, toRemoveAddress;
for (; iter; iter = iter->address)
if (iter->data == dataToRemove) {
toRemoveAddress = (void*)iter;
nextAddress = iter->address;
}
iter = head;
for (; iter; iter = iter->address)
if (iter->address == toRemoveAddress)
iter->address = nextAddress;
printf("\nElement: %d was removed successfully", dataToRemove);
return head;
}

void getLastElementAddress(Number *head) {


Number *iter = head;
int addressLastElement;
while (iter->address != NULL) {
addressLastElement = iter->address;
iter = iter->address;
}
printf ("Last element address: %d", addressLastElement);
}

void printList(Number *head) {


int j = 0; Number *iter = head;
printf("\nOriginal list:\n");
for (; iter; iter = iter->address)
printf("Number[%d]: %04d | Address %p: | Next Element Address: %p\n", ++j,
iter->data, (void*)iter, (void*)iter->address);
}

void splitList(Number *head) {


Number *link, *firstList = NULL, *secondList = NULL;
Number *current;
while (head != NULL) {
Number *link = malloc(sizeof(link));
link->data = head->data;
link->address = NULL;
if (head->data % 2 != 0) {
if (firstList == NULL) {
firstList = link;
head = head->address;
continue;
}
else {
current = firstList;
while (current->address != NULL)
current = current->address;
current->address = link;
}
head = head->address;
}
else {
if (secondList == NULL) {
secondList = link;
head = head->address;
continue;
}
else {
current = secondList;
while (current->address != NULL)
current = current->address;
current->address = link;
}
head = head->address;
}
}
printList(firstList);
printList(secondList);
}

Number * joinLists(Number *rightList, Number * leftList) {


int reply;
Number *t1, *t2;
if (rightList == NULL && leftList == NULL)
return(NULL);
else
if (rightList != NULL && leftList == NULL)
return(rightList);
else
if (rightList == NULL && leftList != NULL)
return(leftList);
else
{
t1 = rightList;
while (t1->address != NULL)
{
t1 = t1->address;
}
t1->address = leftList;
return(rightList);
}
}
void readListFromFile(char *fileName){
FILE *file = fopen(fileName, "r+");
int c;
if (file) {
while ((c = getc(file)) != EOF)
printf("%c",c);
}
fclose(file);
}

void writeListToFile(Number * head, char *fileName) {


FILE *file = NULL; int j = 0;
file = fopen(fileName, "w+");
while (head->address != NULL) {
fprintf(file, "Number[%d]: %04d | Address %p: | Next Element Address: %p\n",
++j, head->data, (void*)head, (void*)head->address);
head = head->address;
}
fprintf(file, "Number[%d]: %04d | Address %p: | Next Element Address: %p\n",
++j, head->data, (void*)head, (void*)head->address);
fclose(file);
printf("\nContent writen to file %s", fileName);
}

void freeMemory(Number *head) {


Number *toDelete = NULL; Number *iter = head;
for (; iter; iter = iter->address) {
if (toDelete)
free(toDelete);
toDelete = iter;
}
if (toDelete)
free(toDelete);
remove(FILE_NAME);
}

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

You might also like