0% found this document useful (0 votes)
30 views

LAB SDA N2 Revenko Ivan-1

The document describes a lab report on implementing a singly linked list data structure in C. It includes functions to create nodes, insert nodes at the beginning, end or a given position of the list, delete nodes, find a node by value, reverse and sort the list. The main function provides a menu to call these functions and test the linked list implementation. The conclusion states the purpose was to familiarize students with creating and performing basic operations on singly linked lists using C.

Uploaded by

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

LAB SDA N2 Revenko Ivan-1

The document describes a lab report on implementing a singly linked list data structure in C. It includes functions to create nodes, insert nodes at the beginning, end or a given position of the list, delete nodes, find a node by value, reverse and sort the list. The main function provides a menu to call these functions and test the linked list implementation. The conclusion states the purpose was to familiarize students with creating and performing basic operations on singly linked lists using C.

Uploaded by

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

Universitatea Tehnică a Moldovei

Facultatea Calculatoare Informatică și Microelectronică


Departamentul Ingineria Software și Automatică

RAPORT
la lucrarea de laborator nr.2
la Structuri de date și algoritme

Tema: Implementarea tipului de date abstract “Listă simplu înlățuită” în


limbajul C.

A efectuat: Revenko Ivan TI-224

A verificat: Bîrnaz Adrian

Chişinău 2022
Scopul lucrării
Scopul lucrării este de a familiariza studentul cu mecanismul de creare a listeleor simplu înlățuite
și operații elementare asupra acestuia, utilizînd pentru aceasta limbajul C.

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *next;
};

typedef struct Node Node;

Node *head = NULL;

Node *create_node(int data) {


Node *new_node = (Node*) malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}

void insert_at_end(int data) {


Node *new_node = create_node(data);
if (head == NULL) {
head = new_node;
return;
}
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}

void insert_at_beginning(int data) {


Node *new_node = create_node(data);
new_node->next = head;
head = new_node;
}

void insert_at_position(int data, int position) {


if (position == 1) {
insert_at_beginning(data);
return;
}
Node *new_node = create_node(data);
Node *current = head;
for (int i = 1; i < position - 1; i++) {
if (current == NULL) {
printf("Invalid position!\n");
free(new_node);
return;
}
current = current->next;
}
if (current == NULL) {
printf("Invalid position!\n");
free(new_node);
return;
}
new_node->next = current->next;
current->next = new_node;
}

void delete_at_position(int position) {


if (head == NULL) {
printf("List is empty!\n");
return;
}
Node *current = head;
Node *previous = NULL;
for (int i = 1; i < position; i++) {
if (current == NULL) {
printf("Invalid position!\n");
return;
}
previous = current;
current = current->next;
}
if (previous == NULL) {
head = head->next;
} else {
previous->next = current->next;
}
free(current);
}
int find_position(int value) {
int position = 1;
Node *current = head;
while (current != NULL) {
if (current->data == value) {
return position;
}
current = current->next;
position++;
}
return -1;
}

void reverse_list() {
Node *previous = NULL;
Node *current = head;
Node *next = NULL;
while (current != NULL) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}
head = previous;
}

void sort_list() {
if (head == NULL || head->next == NULL) {
return;
}
int swapped;
Node *current;
Node *last = NULL;
do {
swapped = 0;
current = head;
while (current->next != last) {
if (current->data > current->next->data) {
int temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}

void print_list() {
if (head == NULL)
{
printf("List is empty!\n");
return;
}
printf("List elements:\n");
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

void free_list() {
Node *current = head;
Node *next = NULL;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
head = NULL;
}

void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}

int main() {
int choice, data, position, value;
do {
printf("\n");
printf("1. Read list from keyboard\n");
printf("2. Print list\n");
printf("3. Find element position\n");
printf("4. Free list\n");
printf("5. Sort list\n");
printf("6. Insert element at end\n");
printf("7. Insert element at beginning\n");
printf("8. Insert element at position\n");
printf("9. Delete element from position\n");
printf("10. Reverse list\n");
printf("11. Clear input buffer\n");
printf("12. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter list elements (-1 to stop):\n");
do {
scanf("%d", &data);
if (data != -1) {
insert_at_end(data);
}
} while (data != -1);
break;
case 2:
print_list();
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &value);
position = find_position(value);
if (position != -1) {
printf("Element found at position %d\n", position);
} else {
printf("Element not found\n");
}
break;
case 4:
free_list();
break;
case 5:
sort_list();
printf("List sorted\n");
break;
case 6:
printf("Enter element to insert at end: ");
scanf("%d", &data);
insert_at_end(data);
break;
case 7:
printf("Enter element to insert at beginning: ");
scanf("%d", &data);
insert_at_beginning(data);
break;
case 8:
printf("Enter position to insert element: ");
scanf("%d", &position);
printf("Enter element to insert: ");
scanf("%d", &data);
insert_at_position(data, position);
break;
case 9:
printf("Enter position to delete element: ");
scanf("%d", &position);
delete_at_position(position);
break;
case 10:
reverse_list();
printf("List reversed\n");
break;
case 11:
clear_input_buffer();
break;
case 12:
printf("Exiting program\n");
break;
default:
printf("Invalid choice, try again\n");
}
} while (choice != 12);
return 0;
}

Concluzie

În acesta laborator la disciplina structure de date și algoritme noi


am familiarizat studentul cu mecanismul de creare a listeleor simplu
înlățuite și operații elementare asupra acestuia, utilizînd pentru
aceasta limbajul C

You might also like