Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct _Node node;
- struct _Node{
- int num;
- node *next;
- };
- node *head;
- void insert(int val1, int val2){
- while(val2--){
- node *inserting = (node*)malloc(sizeof(node));
- inserting->num = val1;
- inserting->next = head->next;
- head->next = inserting;
- }
- return;
- }
- void erase(int val){
- while(val--){
- node *todelete = head->next;
- head->next = head->next->next;
- free(todelete);
- }
- return;
- }
- void move(int value){
- while(value--) head = head->next;
- return;
- }
- void show(){
- node *current = head;
- do{
- printf("%d", current->num);
- current = current->next;
- if(current == head) printf("\n");
- else printf(" ");
- }while(current != head);
- return;
- }
- int main(){
- int x, n;
- char buf[10];
- scanf("%d %d", &x, &n);
- //initialize
- head = (node*)malloc(sizeof(node));
- head->next = head;
- head->num = x;
- //start reading command
- while(n--){
- scanf("%s", buf);
- if(!strcmp(buf, "insert")){
- int val1, val2;
- scanf("%d %d", &val1, &val2);
- insert(val1, val2);
- }
- else if(!strcmp(buf, "erase")){
- int val;
- scanf("%d", &val);
- erase(val);
- }
- else if(!strcmp(buf, "move")){
- int value;
- scanf("%d", &value);
- move(value);
- }
- else if(!strcmp(buf, "show")){
- show();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement