Data Structure and Algorithm Lab Task 04, Assignments of Data Structures and Algorithms

Data Structure and Algorithm Lab Task 03

Typology: Assignments

2020/2021

Uploaded on 03/16/2021

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB # 04 Data Structure & Algorithms Lab
1
Lab Task(s): Perform the following using C++ Programming Language
1. Create a list using linked list, add 5 elements in it manually and finally display all elements
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
void display(node* n){
while(n!=NULL){
cout<<n->data<<" ";
n=n->next;
}}
int main()
{
node *first;
node *second;
node *third;
node *fourth;
node *fifth;
first = new node();
second = new node();
third = new node();
fourth = new node();
fifth = new node();
first->data=4;
first->next= second;
second->data=6;
second->next= third;
third->data=9;
third->next=fourth;
fourth->data = 7;
fourth->next = fifth;
fifth->data = 6;
fifth->next = NULL;
display(first);
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Data Structure and Algorithm Lab Task 04 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Lab Task(s): Perform the following using C++ Programming Language

  1. Create a list using linked list, add 5 elements in it manually and finally display all elements #include using namespace std; struct node{ int data; node next; }; void display(node n){ while(n!=NULL){ cout<<n->data<<" "; n=n->next; }} int main() { node *first; node *second; node *third; node *fourth; node *fifth;

first = new node(); second = new node(); third = new node(); fourth = new node(); fifth = new node();

first->data=4; first->next= second;

second->data=6; second->next= third;

third->data=9; third->next=fourth;

fourth->data = 7; fourth->next = fifth;

fifth->data = 6; fifth->next = NULL;

display(first); }

  1. Determine size of list in task 1 #include using namespace std;

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

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

int linklistsize(Node head){ int size = 0; Node current = head; while (current != NULL) { size++; current = current->next; } return size; }

int main() { system ("color f0"); Node * head = NULL; Node * second = NULL; Node * third = NULL; Node * fourth = NULL; Node * tail = NULL;

head = new Node(); second = new Node(); third = new Node(); fourth = new Node(); tail = new Node();

  1. Ask user to enter list items and display all the entered list elements when user entered -1.

#include using namespace std; struct node { int data; node* next; }; void printlinkedlist(node* node) { int c=0; //taken just for good looking output while(node!=NULL) { if(c>0) { cout<<"->"<<node->data; node = node->next; } else { cout<<node->data; node = node->next; c++; } } } int main() { system("color f0"); int n; cout<<"Enter no. of nodes="; cin>>n; int num,c=0; node* head = new node; node * temp = new node; cin>>num; head->data=num; for(int i=2;i<=n;i++) { if(c==0) { cin>>num; temp->data=num; head->next=temp;

c++; } else { cin>>num; node * temp1 = new node; temp1->data=num; temp->next=temp1; temp=temp1; } } int a; cout<<"For displaying Element of linklist Press 1: ";cin>>a; if(a==1) printlinkedlist(head); }

while(head!=NULL) { cout<<head->data<<" "; head=head->next; } cout<<endl; } int main() { system("color f0"); Node *head = NULL; push(&head,61); push(&head,32); push(&head,60); push(&head,59); push(&head,61); push(&head,30); push(&head,54); push(&head,19); cout<<"Given Linked List: "; printList(head); cout<<"\nDeleting node "<< head->next->next->data<<" "; deleteNode(head, head->next->next); cout<<"\nModified Linked List: "; printList(head);

cout<<"\nDeleting first node "; deleteNode(head, head);

cout<<"\nModified Linked List: "; printList(head); return 0; }

  1. Create a function which will find specific Item in a linked list #include #include <stdio.h> #include <stdlib.h> using namespace std; struct node { int num; node nextptr; }stnode; void makeList(int n); void searchList(int item, int n); int main() { system("color f0"); int n,num,item; cout<<"Enter the number of nodes: "; cin>>n; makeList(n); cout<<"\nEnter element you want to search: "; cin>>item; searchList(item,n); return 0; } void makeList(int n) //function to create linked list. { struct node *frntNode, *tmp; int num, i; stnode = (struct node *)malloc(sizeof(struct node)); if(stnode == NULL) { cout<<" Memory can not be allocated"; } else { cout<<"Enter the data for node 1: "; cin>>num; stnode-> num = num; stnode-> nextptr = NULL; tmp = stnode; for(i=2; i<=n; i++) { frntNode = (struct node *)malloc(sizeof(struct node)); if(frntNode == NULL) {