OAIP/19-20/20.c

169 lines
2.3 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <Windows.h>
void printList(), clearList(), addToHead(int);
int deleteFromHead(), contains(int);
void oddX10(), iX100(int), lessiX10(int);
typedef struct NODE {
int data;
struct NODE* next;
} Node;
Node* arr = NULL;
int main() {
arr = NULL;
printList();
addToHead(1);
printList();
addToHead(2);
printList();
addToHead(3);
printList();
addToHead(4);
addToHead(5);
printList();
printf("DELETE\n");
deleteFromHead();
printList();
int ans = 0;
scanf_s("%d", &ans);
printf("contains %d %d\n\n", ans, contains(ans));
printf("summ = %d\n\n", sum());
printf("k%%2==0 = %d\n\n", evenCount());
printf("ODD X 10\n");
oddX10();
printList();
scanf_s("%d", &ans);
printf("a[%d] X 100\n", ans);
iX100(ans);
printList();
scanf_s("%d", &ans);
printf("less a[%d] X 10\n", ans);
lessiX10(ans);
printList();
clearList();
printList();
}
void printList() {
Node* ptr = arr;
while (ptr != NULL) {
printf("(%d) -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n\n");
}
void addToHead(int value) {
Node* newNode = (struct Node*)malloc(sizeof(Node));
newNode->next = arr;
newNode->data = value;
arr = newNode;
}
int deleteFromHead() {
int value = arr->data;
Node* delNode = arr;
arr = arr->next;
free(delNode);
return value;
}
int contains(int value) {
Node* ptr = arr;
while (ptr != NULL) {
if (ptr->data == value) {
return 1;
}
ptr = ptr->next;
}
return 0;
}
void clearList() {
while (arr != NULL)
{
Node* delNode = arr;
arr = arr->next;
free(delNode);
}
}
int sum() {
Node* ptr = arr;
int s = 0;
while (ptr != NULL) {
s += ptr->data;
ptr = ptr->next;
}
return s;
}
int evenCount() {
Node* ptr = arr;
int k = 0;
while (ptr != NULL) {
if (ptr->data % 2 == 0) {
k++;
}
ptr = ptr->next;
}
return k;
}
void oddX10() {
Node* ptr = arr;
while (ptr != NULL) {
if (ptr->data % 2 != 0) {
ptr->data *= 10;
}
ptr = ptr->next;
}
}
void iX100(int i) {
Node* ptr = arr;
int index = 0;
while (ptr != NULL) {
if (index == i) {
ptr->data *= 100;
return;
}
index += 1;
ptr = ptr->next;
}
}
void lessiX10(int i) {
Node* ptr = arr;
int index = 0;
while (ptr != NULL) {
if (index < i) {
ptr->data = ptr->data * 10;
}
if (index == i) {
return;
}
index += 1;
ptr = ptr->next;
}
}