Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
2
Indexable
Never
#include<iostream>

using namespace std;

int N, num;
int K, j;
char c;


struct Node{
	int data;
	Node* next;
};

Node pool[30001];
int usedIndex = 0;

void insert(Node* head, int j, int data)
{
	//Node* newNode = new Node();
	//newNode->data = value;
	//newNode->next = NULL;

	////int num = 0;
	//
	//Node* p = head;
	//for(int i = 0; i < pos; i ++)
	//{
	//	if(p->next == NULL){
	//		p->next = newNode;
	//		return;
	//	}
	//	p = p->next;
	//}

	//newNode->next = p->next;
	//p->next = newNode;

	Node* newNode = head;
	while(j-- > 0 && newNode->next != nullptr){
		newNode = newNode->next;
	}

	Node* p = new Node();
	p->data = data;
	p->next = newNode->next;
	newNode->next = p;
}

void earse(Node* head, int j)
{
	/*Node* p = head;
	for(int i = 0; i < index; i ++)
	{
		if(p->next == NULL){
			return;
		}
		p = p->next;
	}
	if(p->next != NULL){
		Node* deleted = p->next;
		p->next = deleted->next;
		delete deleted;
	}*/

	Node* p = head;
	 while(j-->0 && p->next!=nullptr){
           p=p->next;
     }
	 if(j==-1&& p->next!=nullptr){
		 Node* deleted = p->next;
         p->next=deleted->next;
		 delete deleted;
     }

}

void printNode(Node* head, int j)
{
	/*if(head->next == NULL)
		cout << "empty" << " ";
	if(index > num - 1){
		for(Node* p = head->next; p != NULL; p = p->next){
			cout << p->data << " ";
		}
	}
	else{
		Node* p = head;
		for(int i = 0; i < index; i ++)
		{
			p = p->next;
			cout << p->data << " ";
		}
	}*/

	 for (Node* p = head->next; p!=nullptr && j-->0 ;p=p->next ){
            
        cout << p->data << " ";
            
     }
     if(head->next==nullptr){
         cout << "empty ";
         return;
     }

}

int main(int argc, char** argv)
{
	int test_case;
	int T;
	//freopen("input.txt", "r", stdin);
	cin>>T;

	for(test_case = 1; test_case <= T; ++test_case)
	{
		cin >> N;

		Node* head = new Node();
		head->data = -1;
		head->next = nullptr;

		num = 0;
		cout << "#" <<test_case << " ";
		for(int i = 0; i < N; i++)
		{
			cin >> c;

			if( c == 'f')
			{
				cin >> K;
				
				insert(head, 0, K);
			}

			else if(c == 'i')
			{
				cin >> j >> K;
				insert(head, j, K);
			}

			else if(c == 'r')
			{
				earse(head, 0);
			}

			else if(c == 'd')
			{
				cin >> j;
				earse(head, j);
			}

			else if(c == 'p')
			{
				cin >> j;
				printNode(head, j);
			}
		}

		cout << endl;

		//delete(head->next);	
		for (Node* p = head->next; p!=nullptr ;p=p->next ){	

		}
	}

	return 0;//Your program should return 0 on normal termination.
}