Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Skip to content
- Search or jump to…
- Pull requests
- Issues
- Marketplace
- Explore
- @nihalbaig0
- 1
- 0 0 cse-138-2018-a/assignment-1-singly-linked-list-nihalbaig0 Private
- Code Issues 0 Pull requests 0 Projects 0 Wiki Security Insights
- assignment-1-singly-linked-list-nihalbaig0/20190715_singly_linked_list.cpp
- @enamcse enamcse Initial commit
- f34e018 34 seconds ago
- 225 lines (217 sloc) 6.83 KB*/
- #include <iostream>
- using namespace std;
- struct node{
- int val;
- node *next;
- };
- struct SinglyLinkedList{
- node *head,*tail;
- SinglyLinkedList();
- ~SinglyLinkedList();
- /**
- * This function takes an integer parameter
- * and add it after the last element of the
- * singly linked list.
- */
- void enqueue(int x);
- /**
- * This function removes the first element from
- * the singly linked list.
- * @return integer - the removed integer.
- */
- int dequeue();
- /**
- * This function prints the whole singly linked
- * list starting from the header node.
- */
- void printList();
- /**
- * This function insert the provided integer just
- * after the header node. If there is no node
- * exists in the list, it inserts the integer as
- * first element.
- * @param x - integer - to add in the list.
- */
- void insertAfterHead(int x);
- /**
- * This function inserts the provided integer just
- * before the trailer node. If there is no node or
- * one node exists in the list, it inserts the
- * integer as first element.
- * @param x - integer - to add in the list.
- */
- void insertBeforeTail(int x);
- /**
- * This function inserts the provided integer toAdd
- * just before the first occurrence of the value
- * toFind. If the value does not exist, it adds
- * the integer at the end of the list.
- * @param toFind - integer - the value to find.
- * @param toAdd - integer - to add in the list.
- */
- void insertBeforeVal(int toFind, int toAdd);
- /**
- * This function inserts the provided integer val
- * just before the position pos.
- * Example: if the provided position is 4, then
- * the inserting integer should be added in a
- * manner so that the inserted value remains at the
- * position 2 after performing this addition. If the
- * position overflows, then the value should be
- * added at the end of the list. If the position
- * underflows, then the value should be added at
- * the beginning of the list.
- * @param pos - integer - the desired position.
- * @param val - integer - to add in the list.
- */
- void insertBeforePos(int pos, int val);
- /**
- * This function inserts the provided integer toAdd
- * just after the first occurrence of the value
- * toFind. If the value does not exist, it adds
- * the integer at the end of the list.
- * @param toFind - integer - the value to find.
- * @param toAdd - integer - to add in the list.
- */
- void insertAfterVal(int toFind, int toAdd);
- /**
- * This function inserts the provided integer val
- * just after the position pos.
- * Example: if the provided position is 4, then
- * the inserting integer should be added in a
- * manner so that the inserted value remains at the
- * position 5 after performing this addition. If the
- * position overflows, then the value should be
- * added at the end of the list. If the position
- * underflows, then the value should be added at
- * the beginning of the list.
- * @param pos - integer - the desired position.
- * @param val - integer - to add in the list.
- */
- void insertAfterPos(int pos, int val);
- /**
- * This function inserts the provided integer val
- * just at the position pos.
- * Example: if the provided position is 4, then
- * the inserting integer should be added in a
- * manner so that the inserted value remains at the
- * position 4 after performing this addition. If the
- * position overflows, then the value should be
- * added at the end of the list. If the position
- * underflows, then the value should be added at
- * the beginning of the list.
- * @param pos - integer - the desired position.
- * @param val - integer - to add in the list.
- */
- void insertAtPos(int pos, int val);
- /**
- * This function deletes the integer at position
- * pos. If the position does not exist, it prints
- * underflow.
- * @param pos - integer - the desired position.
- */
- int deleteFromPos(int x);
- /**
- * This function deletes the first occurrence of the
- * provided integer x. If the position does not
- * exist, it prints underflow.
- * @param x - integer - the value to be deleted.
- */
- int deleteVal(int x);
- /**
- * This function deletes the header node of the
- * Singly Linked List. If the list is empty, then
- * it prints underflow.
- */
- int deleteHead();
- /**
- * This function deletes the trailer node of the
- * Singly Linked List. If the list is empty, then
- * it prints underflow.
- */
- int deleteTail();
- /**
- * This function deletes the node after the
- * header node of the Singly Linked List. If the
- * list has one element, then it deletes header
- * node. If the list is empty, then it prints
- * underflow.
- */
- int deleteValAfterHead();
- /**
- * This function deletes the node before the
- * trailer node of the Singly Linked List. If the
- * list has one element, then it deletes trailer
- * node. If the list is empty, then it prints
- * underflow.
- */
- int deleteValBeforeTail();
- };
- int SinglyLinkedList::deleteHead(){
- }
- SinglyLinkedList::SinglyLinkedList(){
- head = tail = NULL;
- cout << "Singly Linked List initiated\n";
- }
- SinglyLinkedList::~SinglyLinkedList(){
- cout << "Oh! You killed me!\n";
- }
- void SinglyLinkedList::enqueue(int x){
- node *cur = new node;
- cur->val = x;
- cur->next = NULL;
- cout << x <<" is enqueued!\n";
- if(head==NULL && tail==NULL){
- head = tail = cur;
- return;
- }
- tail->next = cur;
- tail = tail->next; /// tail = cur;
- }
- int SinglyLinkedList::dequeue(){
- if(head == NULL && tail == NULL){
- cout << "Underflow!\n";
- return -1;
- }
- node *cur = head;
- int x = cur->val;
- cout << x <<" is dequeued!\n";
- if(head == tail){
- head = tail = NULL;
- }
- else head = head->next;
- delete cur;
- return x;
- }
- void SinglyLinkedList::printList(){
- cout << "SinglyLinkedList - Start\n";
- node *cur = head;
- while(cur!=NULL)
- {
- cout << cur->val << endl;
- cur = cur->next;
- }
- cout << "SinglyLinkedList - End\n";
- }
- void SinglyLinkedList::insertAfterHead(int x)
- {
- node *cur = new node;
- cur->val = x;
- cur->next = NULL;
- if(head == tail)
- {
- head->next = cur;
- tail = cur;
- }
- else
- {
- cur->next = head->next;
- head->next = cur;
- }
- cout <<cur->val << " inserted after head\n";
- }
- void SinglyLinkedList::insertBeforeTail(int x)
- {
- node *cur = new node,*temp;
- cur->val = x;
- cur->next = NULL;
- temp = head;
- if(head==tail)
- {
- cur->next = head;
- head = cur;
- }
- else
- {
- while(temp->next!=tail)
- {
- temp = temp->next;
- }
- temp->next = cur;
- cur->next = tail;
- }
- cout <<cur->val << " inserted before tail\n";
- }
- void SinglyLinkedList::insertBeforeVal(int toFind, int toAdd)
- {
- node *cur = new node,*temp;
- cur->val = toAdd;
- cur->next = NULL;
- temp = head;
- if(toFind== head->val)
- {
- cur->next = head;
- head = cur;
- }
- else{
- while(temp->val != toFind)
- temp = temp->next;
- cur->next = temp;
- temp = head;
- while(temp->next != cur->next)
- temp = temp->next;
- temp->next = cur;
- }
- cout << toAdd<< " added before " << toFind <<endl;
- }
- void SinglyLinkedList::insertAfterVal(int toFind, int toAdd)
- {
- node *cur = new node,*temp;
- cur->val = toAdd;
- cur->next = NULL;
- temp = head;
- if(toFind== head->val)
- {
- cur->next = head->next;
- head->next = cur;
- }
- else
- {
- while(temp->val != toFind)
- temp = temp->next;
- cur->next = temp->next;
- temp->next = cur;
- }
- cout << toAdd<< " added after " << toFind <<endl;
- }
- void SinglyLinkedList::insertBeforePos(int pos, int val)
- {
- int i;
- node *cur = new node,*temp;
- cur->val = val;
- cur->next = NULL;
- temp = head;
- if(pos<=0)
- {
- cur->next = head;
- head = cur;
- }
- else
- {
- for(i=0;i <pos-2;i++)
- {
- temp=temp->next;
- if(temp->next=tail)
- {
- cur->next = tail;
- temp->next = cur;
- cout << val << " added before position " << pos <<endl;
- return;
- }
- }
- cur->next = temp->next;
- temp->next = cur;
- }
- cout << val << " added before position " << pos <<endl;
- }
- void SinglyLinkedList::insertAfterPos(int pos, int val)
- {
- int i;
- node *cur = new node,*temp;
- cur->val = val;
- cur->next = NULL;
- temp = head;
- if(pos<=0)
- {
- cur->next = head;
- head = cur;
- }
- else
- {
- for(i=0;i <pos-1;i++)
- {
- temp=temp->next;
- if(temp->next==tail)
- {
- temp->next = cur ;
- cur->next = tail;
- cout << val << " added after position " << pos <<endl;
- return;
- }
- }
- cur->next = temp->next;
- temp->next =cur;
- cout << val << " added after position " << pos <<endl;
- }
- }
- void SinglyLinkedList::deleteFromPos(int x)
- {
- int i;
- node *cur = new node,*temp;
- cur->val = val;
- cur->next = NULL;
- temp = head;
- if(pos<=0)
- {
- cout << "Underflow\n";
- }
- else
- {
- for(i=0;i <pos-1;i++)
- {
- temp=temp->next;
- if(temp==NULL)
- {
- cout << "Underflow\n";
- return;
- }
- }
- for(i=0;i < pos-1;i++)
- {
- temp = temp->next;
- }
- }
- int main()
- {
- SinglyLinkedList sl;sl.printList();
- sl.enqueue(35);sl.printList();
- sl.enqueue(46);sl.printList();
- sl.insertAfterHead(100);sl.printList();
- sl.enqueue(98);sl.printList();
- sl.insertBeforeVal(46,600);sl.printList();
- sl.insertAfterVal(46,700);sl.printList();
- sl.insertBeforeTail(199);sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- sl.enqueue(445);sl.printList();
- sl.insertBeforePos(3,225);sl.printList();
- sl.insertAfterPos(2,775);sl.printList();
- sl.dequeue();sl.printList();
- sl.enqueue(7);sl.printList();
- sl.enqueue(75);sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- sl.dequeue();sl.printList();
- return 0;
- }
- /*© 2019 GitHub, Inc.
- Terms
- Privacy
- Security
- Status
- Help
- Contact GitHub
- Pricing
- API
- Training
- Blog
- About
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement