Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct simvol
- {
- char fild;
- struct simvol* next;
- struct simvol* prev;
- };
- struct simvol1
- { struct simvol *f;
- struct simvol1 *ptr;
- };
- struct simvol* movetoend(struct simvol*source)
- {
- struct simvol*res;
- res=source;
- while(res->next!=NULL)
- res=res->next;
- source=res;
- return res;
- }
- struct simvol* movetoroot(struct simvol*source) //+
- {
- struct simvol*res;
- res=source;
- while(res->prev!=NULL)
- res=res->prev;
- return res;
- }
- void showlistreversed(struct simvol*source)
- {
- struct simvol*res;
- res=movetoend(source);
- do
- {
- if(res==source)
- printf("!");
- printf("%c",res->fild);
- if(res==source)
- printf("!");
- res=res->prev;
- }while(res!=NULL);
- printf("\n");
- }
- struct simvol* init(char lit) //+
- {
- struct simvol*res;
- res=(struct simvol*)malloc(sizeof(struct simvol));
- if(res==NULL)
- {
- printf("ERR:pamyat ne videlelas\n");
- exit;
- }
- res->fild=lit;
- res->next=NULL;
- res->prev=NULL;
- return res;
- }
- struct simvol* add(struct simvol*source,char lit) //+
- {
- struct simvol*res,*temp;
- res=(struct simvol*)malloc(sizeof(struct simvol));
- if(res==NULL)
- {
- printf("ERR:pamyat ne videlelas\n");
- exit;
- }
- res->fild=lit;
- if(source!=NULL)
- if(source->next!=NULL)
- {
- res->prev=source;
- temp=source->next;
- source->next=res;
- res->next=temp;
- temp->prev=res;
- }
- else
- {
- res->prev=source;
- source->next=res;
- res->next=NULL;
- }
- return res;
- }
- void showlist1(struct simvol*source) //+
- {
- struct simvol*res;
- res=movetoroot(source);
- if(res->fild!=NULL)
- do
- {
- printf(" %c ",res->fild);
- res=res->next;
- }while(res!=NULL);
- else
- printf("Vnutri pusto");
- printf("\n");
- }
- void showlist(struct simvol*source) //+
- {
- struct simvol*res;
- res=movetoroot(source);
- do
- {
- if(res==source)
- printf("!");
- printf("%c",res->fild);
- if(res==source)
- printf("!");
- res=res->next;
- }while(res!=NULL);
- printf("\n");
- }
- struct simvol* del(struct simvol*source) //+
- {
- struct simvol*res,*temp;
- if((source->next!=NULL)&&(source->prev!=NULL)) //+
- {
- res=source->prev;
- res->next=source->next;
- temp=source->next;
- temp->prev=res;
- source->next=NULL;
- source->prev=NULL;
- free(source);
- return res;
- }
- if((source->next==NULL)&&(source->prev!=NULL)) //+
- {
- res=source->prev;
- res->next=NULL;
- source->prev=NULL;
- free(source);
- return res;
- }
- if (source->prev==NULL)
- {
- if (source->next==NULL)
- {
- source->fild='\0';
- }
- else
- {
- res=source->next;
- res->prev=NULL;
- source->next=NULL;
- free(source);
- return res;
- }
- }
- }
- struct simvol* start() //+
- {
- struct simvol*res;
- char lit;
- getchar();
- printf("Vvedite 1 element spiska: ");
- scanf("%c",&lit);
- res=init(lit);
- showlist(res);
- showlistreversed(res);
- return res;
- }
- bool voidcheck(struct simvol*source)
- {
- if((source->fild=='\0')&&(source->next==NULL)&&(source->prev==NULL))
- return true;
- else
- return false;
- }
- struct simvol* clean(struct simvol*source)
- {
- struct simvol*res;
- if(!voidcheck(source))
- {
- res=movetoend(source);
- while(res->prev!=NULL)
- res=del(res);
- res->fild='\0';
- printf("\nSpisok ochishen\n");
- return res;
- }
- else
- {
- printf("ERR:spisok i tak pust\n");
- }
- }
- bool rootcheck(struct simvol* source)
- {
- if((source->prev==NULL)&&(source->fild!='\0'))
- return true;
- else
- return false;
- }
- bool endcheck(struct simvol* source)
- {
- if((source->next==NULL)&&(source->fild!='\0'))
- return true;
- else
- return false;
- }
- struct simvol* moveforward(struct simvol*source)
- {
- if(!endcheck(source))
- return source->next;
- else
- {
- printf("ERR:vy nahodites v konce spiska\n");
- return source;
- }
- }
- struct simvol* movebackward (struct simvol* source)
- {
- if(!rootcheck(source))
- return source->prev;
- else
- {
- printf("ERR:vy nahodites v nachale spiska\n");
- return source;
- }
- }
- void prevelemshow(struct simvol*source)
- {
- struct simvol* res;
- if (!rootcheck(source))
- {
- res=source->prev;
- printf("%c\n",res->fild);
- }
- else
- printf("ERR:vy nahodites v nachale spiska\n");
- }
- void nextelemshow(struct simvol*source)
- {
- struct simvol*res;
- if(!endcheck(source))
- {
- res=source->next;
- printf("%c\n",res->fild);
- }
- else
- printf("ERR:vy nahodites v konce spiska\n");
- }
- struct simvol* prevelemdel(struct simvol*source)
- {
- struct simvol* res;
- if(!rootcheck(source))
- {
- res=source->prev;
- source=source->prev;
- res=del(res);
- showlist(res->next);
- showlistreversed(res->next);
- return res->next;
- }
- else
- {
- printf("ERR:vy nahodites v nachale spiska\n");
- return source;
- }
- }
- struct simvol* nextelemdel(struct simvol*source)
- {
- struct simvol*res;
- if(!endcheck(source))
- {
- res=source->next;
- res=del(res);
- showlist(res);
- showlistreversed(res);
- return
- res;
- }
- else
- {
- printf("ERR: vy nahodites v konce spiska");
- return source;
- }
- }
- char prevelemtake(struct simvol*source)
- {
- struct simvol*res;
- if(!rootcheck(source))
- {
- res=source->prev;
- showlist(res);
- showlistreversed(res);
- return res->fild;
- }
- else
- {
- printf("ERR:vy nahodites v nachale spiska\n");
- return '\0';
- }
- }
- char nextelemtake(struct simvol*source)
- {
- struct simvol*res;
- if(!endcheck(source))
- {
- res=source->next;
- showlist(res);
- showlistreversed(res);
- return res->fild;
- }
- else
- {
- printf("ERR:vy nahofites v koncs spiska\n");
- return '\0';
- }
- }
- void prevelemchange(struct simvol*source,char a)
- {
- struct simvol*res;
- if(!rootcheck(source))
- {
- res=source->prev;
- res->fild=a;
- showlist(source);
- showlistreversed(source);
- }
- else
- printf("ERR:vy nahodites v nachale spiska\n");
- }
- void nextelemchange(struct simvol *source,char a)
- {
- struct simvol*res;
- if(!endcheck(source))
- {
- res=source->next;
- res->fild=a;
- showlist(source);
- showlistreversed(source);
- }
- else
- printf("ERR:vy nahoditesv konce spiska\n");
- }
- struct simvol* prevelemadd(struct simvol*source,char lit)
- {
- struct simvol*res;
- if(source->fild!='\0')
- if(!rootcheck(source))
- {
- res=source->prev;
- res=add(res,lit);
- res=res->next;
- return res;
- }
- else
- { res=(struct simvol*)malloc(sizeof(struct simvol));
- if(res==NULL)
- {
- printf("ERR:pamyat ne videlelas\n");
- exit;
- }
- res->next=source;
- res->prev=NULL;
- res->fild=lit;
- source->prev=res;
- }
- else
- source->fild=lit;
- return source;
- }
- struct simvol* nextelemadd(struct simvol*source,char lit)
- {
- struct simvol*res;
- if(source->fild!='\0')
- if(!endcheck(source))
- {
- res=source->next;
- res=add(res,lit);
- res=res->prev;
- return res;
- }
- else
- { res=(struct simvol*)malloc(sizeof(struct simvol));
- if(res==NULL)
- {
- printf("ERR:pamyat ne videlelas\n");
- exit;
- }
- res->prev=source;
- res->next=NULL;
- res->fild=lit;
- source->next=res;
- }
- else
- source->fild=lit;
- }
- void listdel(struct simvol* source)
- {
- source=movetoend(source);
- while(source->prev!=NULL)
- source=del(source);
- free(source);
- }
- struct simvol * Menu2(struct simvol * c)
- {
- char b,point[3];
- bool flag=false;
- while(atoi(point)!=21)
- {
- printf("\n");
- printf("1.Nachalo raboty so spiskom\n");
- printf("2.Ochistka spiska\n");
- printf("3.Proverka spiska napustotu\n");
- printf("4.Ustanovka ukazatelya v nachalo spiska\n");
- printf("5.Ustanovka ukazatelya v konec spiska\n");
- printf("6.Proverka dostigenia nachala spiska\n");
- printf("7.Proverka dostigenia konca spiska\n");
- printf("8.Peredvizhenie ukazatelya vpered\n");
- printf("9.Peredvizhenie ukazatelya nazad\n");
- printf("10.Vyvod predydushego elementa\n");
- printf("11.Vyvod sleduyushego elementa\n");
- printf("12.Udalenye predydushego elementa\n");
- printf("13.Udalenye sleduyushego elementa\n");
- printf("14.Vzyatie znachenya predydushego elementa\n");
- printf("15.Vzyatie znachenya sleduyushego elementa\n");
- printf("16.Izmenenye znachenya predydushego elementa\n");
- printf("17.Izmenenye znachenya sleduyushego elementa\n");
- printf("18.Dobavlenye elementa pered ukazatelem\n");
- printf("19.Dobavlenye elementa\n");
- printf("20.Vyvod spiska na ekran\n");
- printf("21.Konec raboty so spiskom\n");
- printf("\n");
- printf("Vvedite chislo: ");
- scanf("%s",point);
- if((atoi(point))&&(atoi(point)<=22)&&(atoi(point)>=1))
- {
- if(atoi(point)==1)
- {
- flag=true;
- if(c==NULL){
- c=(struct simvol*)malloc(sizeof(struct simvol));
- c->fild='\0';
- c->next=NULL;
- c->prev=NULL; }
- }
- if((atoi(point)==2)&&(flag))
- c=clean(c);
- if((atoi(point)==3)&&(flag))
- if(voidcheck(c))
- printf("spisok pust\n");
- else
- printf("spisok ne pust\n");
- if((atoi(point)==4)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else{
- c=movetoroot(c);
- showlist(c);
- showlistreversed(c);}
- }
- if((atoi(point)==5)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else{
- c=movetoend(c);
- showlist(c);
- showlistreversed(c); }
- }
- if((atoi(point)==6)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else{
- if(rootcheck(c))
- printf("nachalo dostignuto\n");
- else
- printf("nachalo ne dostignuto\n"); }}
- if((atoi(point)==7)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else{
- if(endcheck(c))
- printf("konec dostignut\n");
- else
- printf("konec ne dostignut\n");
- } }
- if((atoi(point)==8)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else{
- c=moveforward(c);
- showlist(c);
- showlistreversed(c);
- } }
- if((atoi(point)==9)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else {
- c=movebackward(c);
- showlist(c);
- showlistreversed(c);
- } }
- if((atoi(point)==10)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else
- prevelemshow(c);}
- if((atoi(point)==11)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else
- nextelemshow(c);
- }
- if((atoi(point)==12)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else
- prevelemdel(c);
- }
- if((atoi(point)==13)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else
- nextelemdel(c);
- }
- if((atoi(point)==14)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else{
- b=prevelemtake(c);
- printf("%c\n",b); //для демонстрации
- }
- }
- if((atoi(point)==15)&&(flag))
- {if(voidcheck(c))
- printf("spisok pust\n");
- else {
- b=nextelemtake(c);
- printf("%c\n",b); //длядемонстрации
- }
- }
- if((atoi(point)==16)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else {
- getchar(); //для демонстрации
- printf("Vvedite novoe znachenye\n"); //для демонстрации
- b=getchar(); //для демонстрации
- prevelemchange(c,b);
- } }
- if((atoi(point)==17)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else {
- getchar(); //для демонстрации
- printf("Vvedite novoe znachenye\n"); //для демонстрации
- b=getchar(); //для демонстрации
- nextelemchange(c,b);
- } }
- if((atoi(point)==18)&&(flag))
- {
- getchar(); //для демонстрации
- printf("Vvedite novyi element\n");
- b=getchar(); //для демонстарции
- prevelemadd(c,b);
- showlist(c);
- showlistreversed(c);
- }
- if((atoi(point)==19)&&(flag))
- {
- getchar(); //для демонстрации
- printf("Vvedite novyi element\n");
- b=getchar(); //для демонстарции
- nextelemadd(c,b);
- showlist(c);
- showlistreversed(c);
- }
- if((atoi(point)==20)&&(flag))
- { if(voidcheck(c))
- printf("spisok pust\n");
- else {
- showlist(c);
- showlistreversed(c);} }
- if((atoi(point)==21)&&(flag))
- {
- flag=false;
- }
- if(flag!=true)
- printf("Nachnite rabotu so spiskom\n");
- }
- else
- printf("Nekorrektnyi vvod\n");
- }
- c=movetoroot(c);
- return c;
- }
- struct simvol1 * nach;
- void listprint (struct simvol1 *hight) {
- struct simvol1 *h;
- h=hight;
- int i=1;
- printf("\n");
- while(h->ptr!=NULL)
- { printf("%d: ",i);
- if(h->f!=NULL)
- showlist1(h->f);
- else
- printf("Vnutri pusto\n");
- printf("\n");
- h=h->ptr;
- i++;
- }
- printf("%d: ",i);
- if(h->f!=NULL)
- showlist1(h->f);
- else
- printf("Vnutri pusto\n");
- printf("\n");
- }
- void ukaz (struct simvol1 *c) {
- struct simvol1 *pop;
- int i;
- i = 1;
- pop = nach;
- if (pop != c) {
- i++;
- while ((pop -> ptr) != c) {
- pop = pop -> ptr;
- i++;
- }
- }
- printf ("\nUkazatel na %d elemente\n", i);
- }
- void add (struct simvol1 *c ) {
- struct simvol1 *temp;
- int i=0;
- temp = (struct simvol1 *) malloc(sizeof(struct simvol1));
- if (temp == NULL)
- printf ("pamyt ne vydelilas\n");
- temp->f=NULL;
- temp->ptr=NULL;
- if(c->f!=NULL)
- if (c ->ptr != NULL) {
- temp -> ptr = c -> ptr;
- c -> ptr = temp;
- temp->f=NULL;
- temp->f=Menu2(temp->f);
- listprint (nach);
- ukaz (c);
- }
- else {
- c -> ptr=temp;
- temp->f=Menu2(temp->f);
- temp->ptr=NULL;
- listprint (nach);
- ukaz (c);
- }
- else
- c -> f =Menu2(c->f);
- listprint (nach);
- }
- struct simvol1 *deletelem (struct simvol1 *c, struct simvol1 *root) {
- struct simvol1 *temp;
- if (c == root) {
- root = root -> ptr;
- free (c);
- return (root);
- }
- else {
- temp = root;
- while (temp -> ptr != c)
- temp = temp -> ptr;
- temp -> ptr = c -> ptr;
- free (c);
- temp = temp -> ptr;
- return (temp);
- }
- }
- struct simvol1 *clean (struct simvol1 *root) {
- struct simvol1 *tmp, *tmp1;
- tmp = root -> ptr;
- if (root -> ptr != NULL)
- while (tmp -> ptr != NULL) {
- tmp1 = tmp;
- tmp = tmp -> ptr;
- free (tmp1);
- }
- root -> ptr = NULL;
- root -> f = NULL;
- return root;
- }
- int voidcheck (struct simvol1 *root) {
- if ((root -> ptr == NULL) && (root -> f == NULL)) {
- printf ("spisok pust\n");
- return 1;
- }
- else {
- printf ("spisok ne pust\n");
- return 0;
- }
- }
- struct simvol1 *movetostart (struct simvol1 *c, struct simvol1 *root) {
- c = root;
- listprint (root);
- ukaz (c);
- return c;
- }
- int endcheck (struct simvol1 *c) {
- if (c -> ptr == NULL)
- return 1;
- else
- return 0;
- }
- struct simvol1 *moveforward (struct simvol1 *c) {
- if (!(endcheck (c))) {
- c = c -> ptr;
- listprint (nach);
- ukaz (c);
- return c;
- }
- else {
- printf ("Oshibka-vy nahodytes v konce spiska\n");
- return c;
- }
- }
- void nextelemprint (struct simvol1 *c) {
- struct simvol1 *tmp;
- if (!endcheck (c)) {
- tmp = c -> ptr;
- showlist1(tmp->f);
- listprint (nach);
- ukaz (c);
- }
- else
- printf ("Oshibka-vy nahodytes v konce spiska\n");
- }
- void nextelemdelete (struct simvol1 *c, struct simvol1 *root) {
- struct simvol1 *tmp;
- if (!endcheck (c)) {
- tmp = c -> ptr;
- deletelem (tmp, root);
- listprint (nach);
- ukaz (c);
- }
- else
- printf ("Oshibka-vy nahodytes v konce spiska\n");
- }
- void nextelemtake (struct simvol1 *c) {
- struct simvol1 *tmp;
- if (!endcheck (c)) {
- tmp = c -> ptr;
- showlist1(tmp->f);
- listprint (nach);
- ukaz (c);
- }
- else {
- printf ("Oshibka-vy nahodytes v konce spiska\n");
- }
- }
- void nextelemchange (struct simvol1 *c) {
- struct simvol1 *tmp;
- if (!endcheck (c)) {
- tmp = c -> ptr;
- tmp->f=Menu2(tmp->f);
- listprint (nach);
- ukaz (c);
- }
- else
- printf ("Oshibka-vy nahodytes v konce spiska\n");
- }
- void end (struct simvol1 *root) {
- struct simvol1 *ud;
- while (root -> ptr != NULL) {
- ud = root;
- root = root -> ptr;
- free (ud);
- }
- free (root);
- }
- struct simvol1 *mtd (struct simvol1 *root) {
- struct simvol1 *c;
- c = root;
- while (c -> ptr != NULL)
- c = c -> ptr;
- return c;
- }
- void menu () {
- int flag,i=0;
- struct simvol1 *root, *c;
- char s, p[3];
- flag = 0;
- while (atoi (p) != 13) {
- printf("\n");
- printf ("1.Nachalo raboty so spiskom\n");
- printf ("2.Ochistka spiska\n");
- printf ("3.Proverka spiska na pustotu\n");
- printf ("4.Ustanovka ukazatelya v nachalo spiska\n");
- printf ("5.Proverka dostigenia koncaspiska\n");
- printf ("6.Peredvizhenie ukazatelya vpered\n");
- printf ("7.Vyvod sleduyushego elementa\n");
- printf ("8.Udalenye sleduyushego elementa\n");
- printf ("9.Vzyatie znachenya sleduyushego elementa\n");
- printf ("10.Izmenenye znachenya sleduyushego elementa\n");
- printf ("11.Dobavlenye elementa\n");
- printf ("12.Vyvod spiska na ekran\n");
- printf ("13.Konec raboty so spiskom\n");
- printf("\n");
- printf ("Vvedite chislo: ");
- scanf ("%s", p);
- if ((atoi (p)) && (atoi(p) <= 13) && (atoi(p) >= 1)) {
- if (atoi (p) == 1) {
- flag = 1;
- c = (struct simvol1 *) malloc(sizeof(struct simvol1));
- c -> f= NULL;
- c -> ptr = NULL;
- nach = c;
- root = c;
- }
- if ((atoi (p) == 2) && (flag))
- if (!voidcheck (root))
- clean (root);
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 3) && (flag))
- voidcheck(root);
- if ((atoi (p) == 4) && (flag))
- if (!voidcheck (root))
- c = movetostart (c, root);
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 5) && (flag))
- if (!voidcheck (root)) {
- if (endcheck (c))
- printf ("konec dostignut\n");
- else
- printf ("konec ne dostignut\n");
- }
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 6) && (flag))
- if (!voidcheck (root))
- c = moveforward (c);
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 7) && (flag))
- if (!voidcheck (root))
- nextelemprint (c);
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 8) && (flag))
- if (!voidcheck (root))
- nextelemdelete (c, root);
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 9) && (flag))
- if (!voidcheck (root)) {
- nextelemtake (c);
- nextelemdelete (c, root);
- }
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 10) && (flag))
- if (!voidcheck (root)) {
- nextelemchange (c); // ......
- }
- else
- printf ("nazmite 11\n");
- if ((atoi (p) == 11) && (flag)) {
- add (c); // .......
- }
- if ((atoi (p) == 12) && (flag)) {
- if (!voidcheck (root)){
- listprint (root);
- ukaz (c); }
- else
- printf ("spisok pust nazmite 11\n");
- }
- if ((atoi (p) == 13) && (flag))
- end (root);
- if (flag != 1)
- printf ("Nachnite rabotu so spiskom\n");
- }
- else
- printf ("Nekorrektnyi vvod\n");
- }
- }
- int main () {
- menu ();
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement