0% found this document useful (0 votes)
17 views9 pages

10 Umair Sarwar 221728

The document discusses a lab experiment on linked lists. It includes code to create a linked list using different functions like push, insertAfter and append. It also includes functions to delete a node from the tail and to count the number of nodes in the linked list.

Uploaded by

UMAIR Khan
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)
17 views9 pages

10 Umair Sarwar 221728

The document discusses a lab experiment on linked lists. It includes code to create a linked list using different functions like push, insertAfter and append. It also includes functions to delete a node from the tail and to count the number of nodes in the linked list.

Uploaded by

UMAIR Khan
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/ 9

AIR UNIVERSITY

DEPARTMENT OF MECHATRONICS ENGINEERING

EXPERIMENT NO 10

Lab Title: Linked list.


Student Name:. Umair Sarwar . Reg. No:. 221728 .

Objective: In this lab we will learn:


□ Base and derived classes
□ Public
inheritance □
Private
inheritance
□ Protected inheritance.
LAB ASSESSMENT:

Attributes Excellent Good Average Satisfactory (2) Unsatisfactory (1)


(5) (4) (3)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and
follows the lab safety
rules
Total Marks Obtained Marks:

LAB REPORT ASSESSMENT:

Attributes Excellent Good Average Satisfactory (2) Unsatisfactory (1)


(5) (4) (3)

Data presentation

Experimental results
Conclusion

Total Marks: Obtained Marks:


QUESTION NUMBER :01
Make a function with name add_link. Whenever you call this function in the main, it will create a new
node containing data.
CODE:

#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
};
void push(Node** head_ref, int new_data)
{

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}
void insertAfter(Node* prev_node, int new_data)
{

if (prev_node == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = prev_node->next;

prev_node->next = new_node;
}

void append(Node** head_ref, int new_data)


{

Node* new_node = new Node();

Node *last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;

last->next = new_node;
return;
}

void printList(Node *node)


{
while (node != NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}

int main()
{

Node* head = NULL;

append(&head, 6);

push(&head, 29);

push(&head, 11);

append(&head, 14);
insertAfter(head->next, 50);

cout<<"Created Linked list is: ";


printList(head);

return 0;
}

OUTPUT

QUESTION NUMBER 2:
Make a function which deletes node from your linked list at
the tail and write a function to display all the data of your
new linked list

CODE:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct
Node));
newnode->data = newdata;
newnode->prev = NULL;
newnode->next = head;
if(head != NULL)
head->prev = newnode ;
head = newnode;
}
void display() {
struct Node* ptr;
ptr = head;
while(ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The doubly linked list is: ";
display();
return 0;
}

OUTPUT:
No output.

QUESTION NUMBER 3:

Write and test a function to count the number of nodes in


the linked list.

CODE:

#include<iostream>
using namespace std;
#include<cstdlib>

struct Node {
int data;
Node* next;
Node(int x)
{
data = x;
next = NULL;
}
};
struct Node* push(struct Node* last, int data)
{
if (last == NULL) {
struct Node* temp
= (struct Node*)malloc(sizeof(struct Node));

temp->data = data;
last = temp;
temp->next = last;

return last;
}

struct Node* temp


= (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = last->next;
last->next = temp;

return last;
}

int counter(Node* head)


{
Node* temp = head;
int result = 0;
if (head != NULL) {
do {
temp = temp->next;
result++;
} while (temp != head);
}

return result;
}

int main()
{
Node* First = NULL;
First = push(First, 1);
First = push(First, 2);
First = push(First, 3);
First = push(First, 4);
First = push(First, 5);
First = push(First, 7);
cout << counter(First);
return 0;
}

CONCLUTION:
In this lab we learn the use of inheritance in classes. We can learn about
public, private, and protected inheritance. also, we learn about constructor
and destructor and learn about methods to use constructor, destructor in
inheritance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy