0% found this document useful (0 votes)
43 views7 pages

1A

This C++ program implements a singly linked list with functions to insert nodes at the beginning, end, or middle of the list, delete nodes from the beginning, end, or middle, and print the list. The main function contains a menu loop that calls the various functions based on user input and ends the program when the user selects option 8. Node structures contain data and next pointer fields, and global variables track the head and tail pointers and number of nodes.

Uploaded by

Messay Damtew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

1A

This C++ program implements a singly linked list with functions to insert nodes at the beginning, end, or middle of the list, delete nodes from the beginning, end, or middle, and print the list. The main function contains a menu loop that calls the various functions based on user input and ends the program when the user selects option 8. Node structures contain data and next pointer fields, and global variables track the head and tail pointers and number of nodes.

Uploaded by

Messay Damtew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

// Mesafint Ayele

// UGR/23035/13

#include <iostream>

using namespace std;

struct Node{

int num;

Node *next;

};

Node *head = NULL;

Node *tail = NULL;

int nodeCounter;

Node* create_num(){

Node* newNode = new Node;

cout << "\tEnter num: ";

cin >> newNode->num;

newNode->next = NULL;

nodeCounter++;

return newNode;

void insertFirst(){

Node* ptr = create_num();

if(head == NULL){

head = tail = ptr;

} else {

Node* temp = head;

ptr->next = temp;

head = ptr;
} cout << "Item added\n\n";

void insertEnd(){

Node* ptr = create_num();

if (head == NULL){

head = tail = ptr;

} else {

tail->next = ptr;

tail = ptr;

} cout << "Item added\n\n";

void insertMid(){

int pos;

cout << "Enter Pos: ";

cin >> pos;

if (pos == 1){

insertFirst();

} else if (pos == nodeCounter + 1){

insertEnd();

} else {

Node* ptr = create_num();

Node* temp = head;

pos--;

while (--pos){

temp = temp->next;

ptr->next = temp->next;

temp->next = ptr;

cout << "Item added\n\n";

}
}

void delete_first(){

if(head == NULL){

cout << "Empty list!\n";

} else {

Node* temp = head;

head = head->next;

if (head == NULL){

tail = NULL;

delete temp;

nodeCounter--;

cout << "Item deleted!\n\n";

void delete_last(){

if (head == NULL){

cout << "Empty list!\n\n";

} else {

nodeCounter--;

Node* deletetail = tail;

Node* temp = head;

if (nodeCounter == 0){

head = NULL;

tail = NULL;

delete deletetail;

} else{

cout << "else";

while(temp->next->next != NULL){

temp = temp->next;
}

delete deletetail;

tail = temp;

tail->next = NULL;

cout << "Item deleted!\n\n";

void deleteAtMid(){

int pos;

cout << "Select number: ";

cin >> pos;

if (pos == 1){

delete_first();

} else if (pos == nodeCounter + 1){

delete_last();

} else {

Node* temp = head;

pos--;

while (--pos){

temp = temp->next;

Node* deleteItem = temp->next;

temp->next = deleteItem->next;

nodeCounter--;

delete deleteItem;

cout << "\nItem deleted!\n\n";

}
void printForward(){

if (head == NULL){

cout << "No num!\n";

} else {

Node* temp = head;

while(temp != NULL){

cout << temp->num << " ";

temp = temp->next;

cout << endl << endl;

bool _end = false;

void user_menu(){

cout << "1. Enter num at first\n"

<< "2. Enter num at last\n"

<< "3. Enter at the mid\n"

<< "4. Delete first num\n"

<< "5. Delete last num\n"

<< "6. Delete num at mid\n"

<< "7. Print num Forward\n"

<< "8. End the program\n"

<< "Enter number: ";

char num;

cin >> num;

cout << endl;

if(num > '8' || num < '0'){

delete &num;

int num = '4';


}

switch (num) {

case '1':

insertFirst();

break;

case '2':

insertEnd();

cout << endl;

break;

case '3':

insertMid();

break;

case '4':

delete_first();

break;

case '5':

delete_last();

break;

case '6':

deleteAtMid();

cout << endl;

break;

case '7':

printForward();

break;

case '8':

_end = true;

break;

default:

_end = true;

break;

}
}

int main(){

while(!_end){

user_menu();

return 0;

You might also like