21BCA1946 Worksheet1.3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment 1.

Student Name: Divya Prakash Singh UID: 21BCA1946


Branch: BCA Section/Group: 21PH-BCA 5B
Semester: 3rd semester Date of Performance: 22/09/2022
Subject Name: DS lab Subject Code: 21CAP-214

1. Aim/Overview of the practical: Write a program to perform the insertion at the


beginning of the liked list.

2. Task to be done: Creating a linked list and inserting an element on the front of it.

3. Concept used: Linked list, operations on linked list, time complexity.

4. Steps/Commands involved to perform practical:


#include <iostream>
using namespace std;

class Node{
public:
int data;
Node * next;
};

void insertStart(Node** prev_head, int new_value)


{
Node * new_head = new Node();
new_head->data = new_value;
new_head->next = (*prev_head);

(*prev_head) = new_head;
}

void printList(Node* a)
{
while (a != NULL)
{
cout << a->data << " ";
a = a->next;
}
}

int main(int argc, char const *argv[])


{
cout << "Name: Divya Prakash Singh" << endl << "UID: 21BCA1946" << endl << endl;
int n;
cout << "Enter list size: ";
cin >> n;

Node* list[n];
Node** tail;
Node** head;

for (int i = 0; i < n; i++)


{
list[i] = new Node();
}

for (int i = 0; i < n; i++)


{
cout << "Enter data for element " << i + 1 << ": ";
cin >> list[i]->data;

head = &(list[0]);

if (i != (n-1))
{
list[i]->next = list[i+1];
}
else if(i == (n-1))
{
list[i]->next = NULL;
tail = &(list[i]);
}
}

cout << "List before insertion: " << endl;


printList(*head);

int new_head_value;
cout << endl << "Enter the element to insert at beginning: ";
cin >> new_head_value;
insertStart(head, new_head_value);

cout << "List after insertion: " << endl;


printList(*head);
return 0;
}

5. Result/Output/Writing Summary:

Learning outcomes (What I have learnt):

1. I learned to create a linked list.

2. I learned to insert element at the head of the linked list.

3. And I learned to print the linked list.


Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Worksheet 20
2. Demonstration/Performance / 10
Quiz/Viva

Worksheet Rubrics:
Understanding of Experiment 10% of total grade that is 2 marks

Command Description for all concepts covered in experiment 30% of total grade that is 6marks

Steps Involved in question 40% of total grade that is 8 marks

Writing Output/Screenshot 20% of total grade that is 4 marks

You might also like