Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- struct student //stack
- {
- struct student* nextstud; //указатель на следующий элемент стэка
- char surname[50];
- };
- struct student_group //circle
- {
- struct student_group* nextgr; //указатель на следующий элемент кольца
- struct student* head; //указатель на стэк для данного элемента
- int count;
- };
- void addcircle(struct student_group**);
- void deletecircle(struct student_group**);
- void showcircle(struct student_group*);
- int creationstack(struct student**);
- void showstack(struct student*);
- int main()
- {
- int k;
- struct student_group* h = NULL;
- while (1)
- {
- rewind(stdin);
- system("CLS");
- puts("Menu");
- puts("1 - Add element of circle");
- puts("2 - Delete element of circle");
- puts("3 - Show elements of circle");
- puts("4 - Return");
- scanf_s("%d", &k);
- switch (k)
- {
- case 1: addcircle(&h); break;
- case 2: deletecircle(&h); break;
- case 3: showcircle(h); break;
- case 4: return 0;
- default: printf("Error of number");
- }
- }
- }
- void addcircle(struct student_group** h)
- {
- struct student_group* s1, *s2;
- if (!(*h))
- {
- if (!(s1 = (struct student_group*)calloc(1, sizeof(struct student_group))))
- {
- printf("Memory error");
- return;
- }
- s1->count = creationstack(&(s1->head));
- printf("Creation of fisrt component is ending\n");
- s1->nextgr = s1;
- *(h) = s1;
- }
- else
- {
- s1 = (*h)->nextgr; //не работает
- while (s1->nextgr != (*h))
- s1 = s1->nextgr;
- }
- do
- {
- if (!(s2 = (struct student_group*)calloc(1, sizeof(struct student_group))))
- {
- printf("Memory error");
- return;
- }
- s2->count = creationstack(&(s2->head));
- s2->nextgr = s1;
- s1 = s2;
- puts("Do you want to continue(group)?");
- rewind(stdin);
- } while (_getch() == 'y');
- (*h)->nextgr = s2;
- }
- void showcircle(struct student_group* h)
- {
- struct student_group* p;
- int i = 0;
- p = h;
- if (!h)
- {
- printf("Circle is empty\n");
- _getch();
- return;
- }
- do
- {
- printf("Number of group: 15050%d\n", ++i);
- printf("Amount of student in this group: %d\n", p->count);
- showstack(p->head);
- p = p->nextgr;
- }
- while (p != h);
- _getch();
- }
- int creationstack(struct student** s)
- {
- int count = 0;
- struct student* p = (*s);
- do
- {
- if (!(*s = (struct student*)calloc(1, sizeof(struct student))))
- {
- printf("Memory error\n");
- return 0;
- }
- printf("Write the student surname: ");
- rewind(stdin);
- gets((*s)->surname);
- count++;
- (*s)->nextstud = p;
- p = (*s);
- puts("Do you want to continue(surname)?");
- rewind(stdin);
- } while (_getch() == 'y');
- return count;
- }
- void showstack(struct student* s)
- {
- int i = 0;
- if (!s)
- {
- printf("Stack is empty\n");
- return;
- }
- do
- {
- printf("Student #%d: %s\n", ++i, s->surname);
- s = s->nextstud;
- } while (s);
- printf("\n");
- }
- void deletecircle(struct student_group** h)
- {
- struct student_group* p = (*h), * s;
- struct student* s1;
- if (!(*h))
- {
- printf("Circle is empty");
- _getch();
- return;
- }
- if (p->nextgr == (*h))
- {
- s1 = p->head;
- while (s1)
- {
- p->head = s1->nextstud;
- free(s1);
- s1 = p->head;
- }
- free(p);
- printf("Information was deleted\n");
- _getch();
- (*h) = NULL;
- return;
- }
- int countmin = p->count;
- s = p;
- p = p->nextgr;
- while (p != (*h))
- {
- if (p->count < countmin)
- {
- countmin = p->count;
- s = p;
- }
- p = p->nextgr;
- }
- p = s->nextgr; //p в позицию после удаляемого элемента
- s1 = s->head; //s1 - рабочий указатель, который указывает на вершину УДаляемого стэка
- while (s1)
- {
- s->head = s1->nextstud; //вершину УДаляемого стэка перемещаем вниз по стэку
- s1->nextstud = p->head; //теперь s1 - новый элемент НЕудаляемого стэка
- p->head = s1; //перемещаем вершину НЕудаляемого стэка выше на элемент
- s1 = s->head; //рабочий указатель УДаляемого стэка перемещаем на вершину УДаляемого стэка
- (p->count)++;
- }
- p = (*h);
- while (p->nextgr != s) // указатель p становится перед s
- p = p->nextgr;
- if (s == (*h))
- {
- (*h) = s->nextgr;
- p->nextgr = s->nextgr;
- free(s);
- }
- else
- {
- p->nextgr = s->nextgr;
- free(s);
- }
- printf("Information was deleted");
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement