add
This commit is contained in:
parent
872b1fc5bd
commit
a20c34c593
71
a3/a3.c
Normal file
71
a3/a3.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define SIZE 8
|
||||||
|
|
||||||
|
struct pizza{
|
||||||
|
int price;
|
||||||
|
char name[100];
|
||||||
|
};
|
||||||
|
|
||||||
|
void insertSort(struct pizza array[SIZE]){
|
||||||
|
struct pizza temp;
|
||||||
|
for(int i = 1; i < SIZE; i++){
|
||||||
|
if(array[i].price < array[i-1].price){
|
||||||
|
for(int j = i-1; j >= 0; j--){
|
||||||
|
if(array[i].price > array[j].price){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if(array[i].price == array[j].price){
|
||||||
|
if(strcmp(array[i].name, array[j].name) < 0){
|
||||||
|
temp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = temp;
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
temp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = temp;
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
struct pizza array[SIZE];
|
||||||
|
|
||||||
|
array[0].price = 5;
|
||||||
|
strcpy(array[0].name, "Cecko Pizza");
|
||||||
|
|
||||||
|
array[1].price = 1;
|
||||||
|
strcpy(array[1].name, "Hacko Pizza");
|
||||||
|
|
||||||
|
array[2].price = 10;
|
||||||
|
strcpy(array[2].name, "Acko Pizza");
|
||||||
|
|
||||||
|
array[3].price = 5;
|
||||||
|
strcpy(array[3].name, "Acko Pizza");
|
||||||
|
|
||||||
|
array[4].price = 5;
|
||||||
|
strcpy(array[4].name, "Becko Pizza");
|
||||||
|
|
||||||
|
array[5].price = 5;
|
||||||
|
strcpy(array[5].name, "Aacko Pizza");
|
||||||
|
|
||||||
|
array[6].price = 8;
|
||||||
|
strcpy(array[6].name, "Becko Pizza");
|
||||||
|
|
||||||
|
array[7].price = 0;
|
||||||
|
strcpy(array[7].name, "Becko Pizza");
|
||||||
|
|
||||||
|
insertSort(array);
|
||||||
|
|
||||||
|
for(int i = 0; i < SIZE; i++)
|
||||||
|
printf("%d %s\n", array[i].price, array[i].name);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -1,18 +1,69 @@
|
|||||||
#include "a_train.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "a_train.h"
|
||||||
|
struct car* add_car(struct car* first, const char* target){
|
||||||
|
if(!strcmp(target, ""))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
struct car* add_car(struct car* first,const char* target) {
|
if(!first){
|
||||||
return NULL;
|
first = (struct car*)calloc(1, sizeof(struct car));
|
||||||
|
strcpy(first->value, target);
|
||||||
|
first->next = NULL;
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct car* temp = first;
|
||||||
|
|
||||||
|
while(temp->next != NULL)
|
||||||
|
temp = temp->next;
|
||||||
|
|
||||||
|
temp->next = (struct car*)calloc(1, sizeof(struct car));
|
||||||
|
strcpy(temp->next->value, target);
|
||||||
|
temp->next->next = NULL;
|
||||||
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_train(struct car* first) {
|
void print_train(struct car* first){
|
||||||
|
struct car* temp = first;
|
||||||
|
|
||||||
|
while(temp != NULL){
|
||||||
|
printf("%s\n", temp->value);
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cancel_train(struct car* first) {
|
void cancel_train(struct car* first){
|
||||||
|
if(!first)
|
||||||
|
return;
|
||||||
|
|
||||||
|
struct car* temp = first;
|
||||||
|
struct car* next = temp->next;
|
||||||
|
|
||||||
|
while(next != NULL){
|
||||||
|
next = temp->next;
|
||||||
|
free(temp);
|
||||||
|
temp = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
first = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct car* clear_train(struct car* first, const char* target){
|
||||||
|
if(!first || !strcmp(target, ""))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
struct car* clear_train(struct car* first, const char* target) {
|
struct car* temp = first;
|
||||||
return NULL;
|
struct car* next = temp->next;
|
||||||
}
|
|
||||||
|
|
||||||
|
while(temp != NULL){
|
||||||
|
next = temp->next;
|
||||||
|
if(!strcmp(temp->value, target))
|
||||||
|
free(temp);
|
||||||
|
temp = next;
|
||||||
|
if(!first)
|
||||||
|
first = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return first;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user