Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
5.4 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

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

struct List {
	struct Node *head, *tail;
};

List l;

void init(){
	l.head = l.tail = NULL;
}

void printList(int stt){
	struct Node *ptr = l.head;
	int len = 0;

	if(ptr == NULL)
		printf("empty ");
	else{
		while(ptr != NULL){

			if(len > stt)
			{
				break;
			}
			else{
				len++;
				printf("%d ", ptr->data);
				ptr = ptr->next;
			}
		}
	}
}

void insertFirst(int key){
	struct Node *link = (struct Node*) malloc(sizeof(struct Node));
	link->data = key;
	link->next = l.head;
	l.head = link;
}

//void insertLast(int key){
//	struct Node *link = (struct Node*) malloc(sizeof(struct Node));
//	link->data = key;
//	link->next = l.tail;
//	l.tail = link;
//}

void insert_stt(int key, int stt){
	struct Node *ptr = l.head;
	struct Node *previous = (struct Node*) malloc(sizeof(struct Node));
	int len = 0;
	int check = 0;

	while(ptr != NULL){

		if(len == stt){
			check = 1;
			break;
		}
		else{
			previous = ptr;
			len++;
			ptr = ptr->next;
		}
	}
	if(check == 1){
		struct Node *link = (struct Node*) malloc(sizeof(struct Node));
		link->data = key;
		previous->next = link;
		link->next = ptr;
	}
	else
	{
		struct Node *pt = l.head;
		struct Node *pre = (struct Node*) malloc(sizeof(struct Node));
		while(pt != NULL){
			pre = pt;
			pt = pt->next;
		}
		struct Node *link = (struct Node*) malloc(sizeof(struct Node));
		link->data = key;
		pre->next = link;
		link->next = pt;
	}
}

void deleteFirst(){
	struct Node *tempLink = l.head;
	l.head = l.head->next;
	tempLink = NULL;
}

int isEmpty(){
	return l.head == NULL;
}

//int lengthLL(){
//	int length = 0;
//	struct Node *current;
//	for(current = l.head; current != NULL; current = current->next){
//		length++;
//	}
//	return length;
//}

//struct Node* findKey(int key){
//	struct Node* current = l.head;
//	if(l.head == NULL){
//		return NULL;
//	}
//	while(current->data != key){
//		if(current->next == NULL)
//			return NULL;
//		else
//			current = current->next;
//	}
//	return current;
//}

//struct Node* deleteKey(int key){
//	struct Node* current = l.head;
//	struct Node* previous = NULL;
//	if(l.head == NULL)
//		return NULL;
//	while(current->data != key){
//		if(current->next == NULL)
//			return NULL;
//		else{
//			previous = current;
//			current = current->next;
//		}
//	}
//	if(current == l.head)
//		l.head = l.head->next;
//	else
//		previous->next = current->next;
//	return current;
//}

void deleteStt(int stt){
	struct Node* current = l.head;
	struct Node* previous = NULL;
	int len = 0;
	int check = 0;

	while(current != NULL){

		if(len == stt){
			check = 1;
			break;
		}
		else
		{
			previous = current;
			current = current->next;
			len++;
		}
	}
	if(check == 1){
		if(current == l.head)
			l.head = l.head->next;
		else
			previous->next = current->next;
	}
}


//input
char cmline;
int j, k, number;

int main(){
	freopen("sample_input.txt", "r", stdin);

	int testcase;
	scanf("%d", &testcase);

	for(int t=1; t<=testcase; t++){
		init();

		printf("#%d ", t);

		//solve
		scanf("%d", &number);
		for(int i=0; i<number; i++){
			scanf(" %c", &cmline);
			switch (cmline)
			{
			case 'f':
				scanf("%d", &k);
				insertFirst(k);
				break;
			case 'i':
				scanf("%d %d", &j, &k);
				insert_stt(k, j);
				break;
			case 'r':
				if(!isEmpty()){
					deleteFirst();
				}
				break;
			case 'd':
				scanf("%d", &j);
				deleteStt(j);
				break;
			case 'p':
				scanf("%d", &j);
				printList(j);
				break;
			default:
				break;
			}
		}
		printf("\n");
	}

	return 0;
}

//input
210
r
f 9040
r
r
r
i 99 3548
r
r
r
f 11323
r
i 35 4833
r
f 31673
i 172 26924
i 57 15573
f 9161
i 34 23655
r
r
d 161
d 107
i 79 8909
r
r
r
f 1655
r
d 85
f 10291
r
f 23199
i 53 4734
p 438
i 158 467
d 167
d 133
r
r
r
r
d 79
d 153
i 53 29314
r
r
d 8
r
f 6038
p 157
d 208
r
i 176 20328
d 83
r
f 10322
i 126 3557
d 2
d 199
p 401
i 85 5002
p 88
d 172
r
r
f 21425
p 49
d 0
d 33
d 139
r
d 147
d 128
r
f 20671
d 205
f 10808
d 36
r
r
i 75 2161
r
r
f 26154
d 166
f 2168
d 108
d 17
r
f 20159
d 157
i 83 32270
d 162
d 182
d 73
r
d 132
i 92 13031
r
d 170
f 29170
d 140
d 160
d 204
p 76
r
r
f 1018
f 2800
r
r
d 73
i 76 7882
i 105 14474
r
r
r
d 185
r
r
d 137
r
d 110
d 30
r
d 150
i 37 12760
p 108
d 170
i 85 27384
f 12835
r
r
d 126
d 139
f 29617
r
d 189
d 112
i 14 16962
r
r
d 71
r
d 92
i 39 20175
r
i 120 31783
d 13
r
f 25705
d 90
d 75
d 7
f 18443
f 9313
r
r
d 101
i 177 19090
d 33
f 22044
d 62
i 49 28503
f 5997
d 127
d 160
i 55 6077
r
f 15759
i 205 4084
f 6287
i 193 21221
f 22171
r
i 194 7591
f 25205
d 82
r
r
d 152
d 142
d 194
d 98
d 183
f 25087
d 93
r
d 28
f 2943
d 9
r
d 78
r
i 0 21430
r
r
p 353
f 19923
r
f 32726
d 131
r
r
r
r
d 69
r
d 185
d 154
28
r
p 1
f 3
p 2
f 9
p 3
i 1 5
p 4
i 9 7
p 5
r
p 6
d 1
p 7
d 0
p 8
r
p 9
f 13
p 10
r
p 11
r
p 12
r
p 13
r
p 14
//output
#2 empty 3 9 3 9 5 3 9 5 3 7 5 3 7 5 7 7 empty 13 empty empty empty empty
Leave a Comment