Коммит 1
This commit is contained in:
parent
73512dbe73
commit
34703fdb61
146
iii.txt
Normal file
146
iii.txt
Normal file
@ -0,0 +1,146 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node* next;
|
||||
};
|
||||
|
||||
|
||||
struct Node* first = NULL;
|
||||
|
||||
void addToHead(int value) {
|
||||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
|
||||
|
||||
newNode->next = first;
|
||||
newNode->data = value;
|
||||
|
||||
first = newNode;
|
||||
}
|
||||
|
||||
void printList() {
|
||||
struct Node* ptr = first;
|
||||
while (ptr != NULL) {
|
||||
printf("(%d) -> ", ptr->data);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
int deleteFromHead() {
|
||||
int value = first->data;
|
||||
struct Node* delNode = first;
|
||||
first = first->next;
|
||||
free(delNode);
|
||||
return value;
|
||||
}
|
||||
|
||||
int contains(int value) {
|
||||
struct Node* ptr = first;
|
||||
while (ptr != NULL) {
|
||||
if (ptr->data == value) {
|
||||
return 1;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void clearList() {
|
||||
while (first != NULL)
|
||||
{
|
||||
struct Node* delNode = first;
|
||||
first = first->next;
|
||||
free(delNode);
|
||||
}
|
||||
}
|
||||
|
||||
int sum()
|
||||
{
|
||||
struct Node* ptr = first;
|
||||
int s = 0;
|
||||
while (ptr != NULL) {
|
||||
s += ptr->data;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int evenCount()
|
||||
{
|
||||
struct Node* ptr = first;
|
||||
int count = 0;
|
||||
while (ptr != NULL) {
|
||||
if (ptr->data % 2 == 0) {
|
||||
count++;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int oddX10()
|
||||
{
|
||||
struct Node* ptr = first;
|
||||
while (ptr != NULL) {
|
||||
if (ptr->data % 2 != 0) {
|
||||
ptr->data *= 10;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void element100(int i) {
|
||||
struct Node* ptr = first;
|
||||
int index = 0;
|
||||
while (ptr != NULL) {
|
||||
if (index == i) {
|
||||
ptr->data = ptr->data * 100;
|
||||
return;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void leftelement(int i) {
|
||||
struct Node* ptr = first;
|
||||
int index = 0;
|
||||
while (ptr != NULL && index < i) {
|
||||
ptr->data *= 10;
|
||||
ptr = ptr->next;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
printList();
|
||||
|
||||
addToHead(1);
|
||||
addToHead(3);
|
||||
addToHead(6);
|
||||
addToHead(9);
|
||||
printList();
|
||||
|
||||
printf("sum = %d\n", sum());
|
||||
|
||||
printf("even numbers = %d\n", evenCount());
|
||||
|
||||
oddX10();
|
||||
printList();
|
||||
|
||||
element100(0);
|
||||
printList();
|
||||
|
||||
element100(3);
|
||||
printList();
|
||||
|
||||
leftelement(2);
|
||||
printList();
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user