This commit is contained in:
ada-dmitry
2023-03-28 13:22:08 +03:00
parent 1e69e53e91
commit ce57be30e6
5 changed files with 125 additions and 0 deletions
@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "header.h"
void pr(node *tmp){
int i;
if(tmp == NULL){printf("[%p]\n", tmp); return;}
while(tmp){
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
printf("data: [%4ld] s[" , tmp->data);
for(i=0;i<COUN; i++) printf("%c", tmp->s[i]);
printf("]\n");
tmp = tmp->next;
}
printf("---------------------\n");
}
node *add_head(node *head, int hh){
node *tmp=NULL;
int i;
if((tmp=malloc(sizeof(node)))==NULL){
perror("malloc: NULL");
exit(2);
}
tmp->prev=NULL;
tmp->data=hh;
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
if(head==NULL) tmp->next=NULL;
else{
tmp->next=head;
head->prev=tmp;
}
return tmp;
}
node *add_tail(node *head, int hh){
node *tmp=NULL;
node *t = NULL;
int i;
if((tmp=malloc(sizeof(node)))==NULL){
perror("malloc: NULL");
exit(3);
}
tmp->data=hh;
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
tmp->next=NULL;
if(head==NULL) tmp->prev=NULL;
else{
t = head;
while(t->next!=NULL) t = t->next;
t->next=tmp;
tmp->prev=head;
}
return head;
}
node *del_head(node *head){
node *tmp = head;
if(head==NULL) return NULL;
if(head -> next == NULL){
free(head);
return NULL;
}
head = head->next;
head -> prev = NULL;
free(tmp);
return head;
}
node *del_tail(node *list){
node *tail = NULL;
if(list==NULL) return NULL;
if(list -> next == NULL){
free(list);
return NULL;
}
tail = list;
while(tail->next->next) tail = tail->next;
free(tail->next);
tail -> next=NULL;
return list;
}
@@ -0,0 +1,19 @@
#define COUN 40
typedef struct Node {
long int data;
struct Node *next;
struct Node *prev;
char s[COUN];
} node;
void pr(node *tmp);
node *add_head(node *head, int hh);
node *add_tail(node *head, int hh);
node *del_head(node *head);
node *del_tail(node *list);
// node *sort(node *head);
// int lenghth(node *list);
// void writefile(const char *path, node *head);
// node *readfile(const char *path);
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
#include <windows.h>
int main(int args, char *argv[]){
node *head=NULL;
int r;
srand(time(NULL));
while((r=(rand()%100))<90) head = add_head(head,r);
pr(head);
head = del_head(head);
pr(head);
head = add_tail(head, 5);
pr(head);
head = del_tail(head);
pr(head);
}
Binary file not shown.
-30
View File
@@ -1,30 +0,0 @@
'''
print("Введите размер массива: ")
n = int(input())
a = [int(input()) for i in range(n)]
'''
a = [4,3,2,5,1]
b = a.copy()
def sortSelect(arr):
for i in range(len(arr)-1):
ind = i
for j in range(i+1, len(arr)):
if(arr[ind]>arr[j]):
ind = j
arr[i], arr[ind] = arr[ind], arr[i]
return arr
def sortInsert(arr):
for i in range(len(arr)):
j = i-1
x = arr[i]
while((arr[j]>x)and(j>=0)):
arr[j+1] = arr[j]
j -= 1
arr[j+1] = x
return arr
#meow
print(sortSelect(a), sortInsert(b))