relocation

This commit is contained in:
ada-dmitry
2023-04-02 20:49:26 +03:00
parent 6cab6118f1
commit 1124209d64
48 changed files with 0 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,26 @@
#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);
}
// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла)
// Ханойские башни игра написать