Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- struct list{
- int inf;
- list* next;
- list* prev;
- };
- void init(list *&h, list *&t){
- h = t = NULL;
- }
- void print(list *&h, list *&t){
- list* p = h;
- while(p){
- cout << p->inf << " ";
- p = p->next;
- }
- }
- void add_head(list *&h, list *&t, int x){
- list* p = new list;
- p->prev = NULL;
- p->inf = x;
- if(!t){
- p->next = NULL;
- h = t = p;
- }
- else{
- h->prev = p;
- p->prev = t;
- t = p;
- }
- }
- void add_back(list *&h, list *&t, int x){
- list* p = new list;
- p->next = NULL;
- p->inf = x;
- if(!h){
- p->prev = NULL;
- h = t = p;
- }
- else{
- t->next = p;
- p->prev = t;
- t = p;
- }
- }
- void del_all(list *&h, list *&t){
- while(h){
- list* p = h;
- if(p == t)
- h = t = NULL;
- else{
- h = h->next;
- h->prev = NULL;
- }
- delete p;
- }
- }
- list* find(list *&h, list *&t, int x){
- list* p = h;
- while(p){
- if(p->inf == x) break;
- p = p->next;
- }
- return p;
- }
- void insert_before(list *&h, list *&t, list *&r, int x){
- list* p = new list;
- p->inf = x;
- p->next = r;
- p->prev = r->prev;
- if(r == h){
- h->prev = p;
- h = p;//?
- }
- else{
- (r->prev)->next = p;
- r->prev = p;
- }
- }
- int pop_front(list *&h, list *&t){
- int i = h->inf;
- list *r = h;
- h = h->next;
- if(h) h->prev = NULL;
- if(!h) t = NULL;
- delete r;
- return i;
- }
- int pop_back(list *&h, list *&t){
- int i = t->inf;
- list *r = t;
- t = t->prev;
- if(t) t->next = NULL;
- if(!t) h = NULL;
- delete r;
- return i;
- }
- void erase(list *&h, list *&t, list *&r){
- if(r == h && r == t)
- h = t = NULL;
- else if(r == h){
- h = h->next;
- t->next = NULL;
- }
- else if(r == t){
- t = t->prev;
- t->next = NULL;
- }
- else{
- (r->prev)->next = r->next;
- (r->next)->prev = r->prev;
- }
- delete r;
- }
- //insert new element before every minimum
- int main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- printf("Enter the number of elements and new member, then array of numbers\n");
- int n, k; scanf("%d%d", &n, &k);
- int mn = 2E9;
- list *h, *t;
- init(h, t);
- for(int i = 0; i < n; i++){
- int cur; scanf("%d", &cur);
- add_back(h, t, cur);
- if(cur < mn)
- mn = cur;
- }
- list *cur = h;
- for(int i = 0; i < n; i++){
- if(cur->inf == mn)
- insert_before(h, t, cur, k);
- cur = cur->next;
- }
- print(h, t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement