Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<string>
- using namespace std;
- struct node{
- int x;
- struct node* next;
- };
- struct node* create_new_node(){
- struct node* tmp;
- tmp = new node;
- return tmp;
- };
- void print_list(struct node* h){
- struct node* it; //iterator for iterating the whole list;
- it=h; // start from head
- cout<<"List: ";
- while(it != NULL){
- cout<<it->x<<" "; // why it->x is used here instead of it.x?
- it = it->next;
- }
- cout<<endl; //new line
- }
- struct node* list_insert_first(struct node* h, int value){
- struct node* temp;
- temp = create_new_node();
- temp->x = value; // why temp->x is used here instead of temp.x?
- temp->next = h; // because insert-first here
- h = temp; //why this should be done? check this!!
- return h;
- }
- struct node* list_insert_last(struct node* h, int value){
- struct node* temp;
- struct node* it;
- temp = create_new_node();
- temp->x = value; // why temp->x is used here instead of temp.x?
- temp->next = NULL;
- if(h==NULL) {
- return temp; //why?
- }
- it = h; //starting to iterate from head
- while(it->next!=NULL){ //iterating till the last node
- it = it->next;
- }
- it->next = temp; //inserting last node
- return h;
- }
- struct node* list_delete_last(struct node* h){
- if(h==NULL) return h; // check why I did this?
- else if(h->next == NULL) return NULL; // check why I did this? ***
- struct node* it = h->next; // interesting!! instead of initializing it by head, I initialized it by head->next. Why? ***
- struct node* it_1 = h; // What is the role of this iterator?
- while(it->next!=NULL){
- it_1 = it;
- it = it->next;
- }
- it_1->next = NULL; // Why I initialized it_1->next by NULL? ***
- return h;
- }
- struct node* list_delete_first(struct node* h){
- if(h==NULL) return h;
- else{
- return h->next; // interesting!! why? ***
- }
- }
- int main(){
- struct node* head; //starting node of the list
- head = NULL; // at first no value, so nothing in that node(you must initialize head before comparing it!!)
- string s;
- int value;
- while(1){
- cin>>s;
- if(s=="insert-first"){
- cin>>value;
- head = list_insert_first(head, value);
- }
- else if(s=="insert-last"){
- cin>>value;
- head = list_insert_last(head, value);
- }
- else if(s=="delete-last"){
- head = list_delete_last(head);
- }
- else if(s=="delete-first"){
- head = list_delete_first(head);
- }
- else if(s=="exit"){
- break;
- }
- else{
- cout<<"Operation is not identified!"<<endl;
- }
- print_list(head);
- }
- }
- /*
- input:
- insert-first 1
- insert-first 3
- insert-last 2
- insert-last 4
- insert-first 5
- insert-last 6
- insert-first 8
- insert-last 7
- insert-first 1
- insert-last 9
- insert-last 10
- insert-first 11
- delete-last
- delete-last
- delete-first
- delete-first
- delete-first
- delete-last
- delete-first
- delete-last
- delete-first
- delete-first
- delete-last
- delete-last
- delete-first
- delete-last
- delete-first
- insert-first 1
- exit
- */
- /*
- output:
- List: 1
- List: 3 1
- List: 3 1 2
- List: 3 1 2 4
- List: 5 3 1 2 4
- List: 5 3 1 2 4 6
- List: 8 5 3 1 2 4 6
- List: 8 5 3 1 2 4 6 7
- List: 1 8 5 3 1 2 4 6 7
- List: 1 8 5 3 1 2 4 6 7 9
- List: 1 8 5 3 1 2 4 6 7 9 10
- List: 11 1 8 5 3 1 2 4 6 7 9 10
- List: 11 1 8 5 3 1 2 4 6 7 9
- List: 11 1 8 5 3 1 2 4 6 7
- List: 1 8 5 3 1 2 4 6 7
- List: 8 5 3 1 2 4 6 7
- List: 5 3 1 2 4 6 7
- List: 5 3 1 2 4 6
- List: 3 1 2 4 6
- List: 3 1 2 4
- List: 1 2 4
- List: 2 4
- List: 2
- List:
- List:
- List:
- List:
- List: 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement