Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Node
- {
- string data;
- struct Node *next;
- };
- struct Node *head = NULL;
- void CreatAList(int n)
- {
- for(int i=0; i<n; i++)
- {
- string val;
- cin >> val;
- struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
- newNode -> data = val;
- newNode -> next = NULL;
- if(head == NULL)
- {
- head = newNode;
- }
- else
- {
- struct Node *cur;
- cur = head;
- while(cur -> next != NULL)
- {
- cur = cur -> next;
- }
- cur -> next = newNode;
- }
- }
- }
- void insertFirst(string val)
- {
- struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
- newNode -> data = val;
- newNode -> next = NULL;
- if(head == NULL)
- {
- head = newNode;
- }
- else
- {
- newNode -> next = head;
- head = newNode;
- }
- }
- void insertLast(string val)
- {
- struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
- newNode -> data = val;
- newNode -> next = NULL;
- if(head == NULL)
- {
- head = newNode;
- }
- else
- {
- struct Node *cur = head;
- while(cur -> next != NULL)
- {
- cur = cur -> next;
- }
- cur -> next = newNode;
- }
- }
- void insertAt(int pos,string val)
- {
- if(pos==1)
- {
- insertFirst(val);
- return;
- }
- if(head==NULL)
- {
- printf("No linked list is being created\n");
- return;
- }
- struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
- newNode -> data = val;
- newNode -> next = NULL;
- struct Node *temp;
- temp = head;
- for(int i=2; i<pos; i++)
- {
- temp = temp -> next;
- if(temp==NULL) {
- printf("Insertion is not possible\n");
- return;
- }
- }
- newNode -> next = temp -> next;
- temp -> next = newNode;
- }
- void deletefirst()
- {
- if(head == NULL)
- {
- printf("List is empty\n");
- return;
- }
- else
- {
- struct Node *cur;
- cur = head;
- head = head -> next;
- free(cur);
- printf("Successfully Deleted!\n\n");
- }
- }
- void deleteLast()
- {
- if(head == NULL)
- {
- printf("List is empty\n");
- return;
- }
- else
- {
- struct Node *cur, *prev;
- cur = head;
- prev = head;
- while(cur -> next != NULL)
- {
- prev = cur;
- cur = cur -> next;
- }
- if(cur == head) head = NULL;
- else prev -> next = NULL;
- free(cur);
- printf("Successfully Deleted!\n\n");
- }
- }
- void deleteAt(int pos)
- {
- if(head == NULL)
- {
- printf("List is empty\n");
- return;
- }
- else
- {
- struct Node *cur, *prev;
- cur = head;
- prev = head;
- for(int i=1; i<pos; i++)
- {
- if(cur == NULL)
- {
- printf("Deletion is not possible!\n");
- return;
- }
- prev = cur;
- cur = cur -> next;
- }
- if(cur == head) head = NULL;
- else
- {
- prev -> next = cur -> next;
- }
- free(cur);
- printf("Successfully Deleted!\n\n");
- }
- }
- void search(string val)
- {
- if(head==NULL)
- {
- printf("List is empty!\n");
- return;
- }
- int index = 1;
- struct Node *cur;
- cur = head;
- while(cur != NULL)
- {
- if(cur -> data == val)
- {
- cout << val << ' ';
- printf("found at position: %d\n",index);
- return;
- }
- cur = cur -> next;
- index++;
- }
- cout << val << ' ';
- printf("is not found in the list\n");
- }
- void printNode(struct Node* cur)
- {
- printf("Linked List: ");
- while(cur != NULL)
- {
- cout << cur->data << ' ';
- cur = cur -> next;
- }
- printf("\n\n");
- }
- void freeTheList()
- {
- struct Node *curNode = head;
- while(head != NULL)
- {
- curNode = head;
- head = head->next;
- free(curNode);
- }
- }
- int main()
- {
- int n;
- printf("Enter the number of elements in the list: ");
- scanf("%d",&n);
- printf("Enter %d elements: ", n);
- CreatAList(n);
- printNode(head);
- printf("A linked list has been created!\n\n\n");
- while(1)
- {
- printf("1. Insert First\n");
- printf("2. Insert Last\n");
- printf("3. Insert At\n");
- printf("4. Delete First\n");
- printf("5. Delete Last\n");
- printf("6. Delete At\n");
- printf("7. Search\n");
- printf("8. Print List\n");
- printf("9. Exit\n");
- printf("Enter Choice: ");
- int choice;
- scanf("%d",&choice);
- if(choice == 1)
- {
- printf("Enter the element: ");
- string val;
- cin >> val;
- insertFirst(val);
- printf("Successfully inserted!\n\n");
- }
- else if(choice == 2)
- {
- printf("Enter the element: ");
- string val;
- cin >> val;
- insertLast(val);
- printf("Successfully inserted!\n\n");
- }
- else if(choice == 3)
- {
- printf("Enter the position (between first and last): ");
- int pos;
- scanf("%d",&pos);
- printf("Enter the element: ");
- string val;
- cin >> val;
- insertAt(pos,val);
- printf("Successfully inserted!\n\n");
- }
- else if (choice == 4) deletefirst();
- else if (choice == 5) deleteLast();
- else if (choice == 6)
- {
- printf("Enter the position: ");
- int pos;
- scanf("%d",&pos);
- deleteAt(pos);
- }
- else if(choice == 7)
- {
- printf("Enter the value: ");
- string val;
- cin >> val;
- search(val);
- }
- else if(choice == 8) printNode(head);
- else if(choice == 9) break;
- else printf("Wrong Choice Input!!\n\n");
- }
- freeTheList();
- }
Add Comment
Please, Sign In to add comment