Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views16 pages

Datastructure Assignment

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)
4 views16 pages

Datastructure Assignment

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/ 16

THE UNIVERSITY OF DODOMA

COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION


CP 213: INDIVIDUAL ASSIGNMENT REPORT

NAME: STEPHANO ALPHONCE


REGISTRATION NO: T22-03-07750
COURSE: BSc .TE2
ALL CODES ORGANIZED IN CLASS LECTURES
Data structure and algorithm analysis
referencing member of structure using Dot operator
#include<iostream>

using namespace std;

struct person{

string name;

char gender;

float weight;

int age;

};

int main(){

person x;

//referencing member of structure using Dot operator

cout<<"Enter the Name"<<x.name<<endl;

cin>>x.name;

cout<<"Enter the gender "<<x.gender<<endl;

cin>>x.gender;

cout<<"Enter the weight "<<x.weight<<endl;

cin>>x.weight;

cout<<"Enter the age "<<x.age<<endl;

cin>>x.age;

cout<<" Name ="<<x.name<<endl;

cout<<" gender ="<<x.gender<<endl;

cout<<"weight ="<<x.weight<<endl;

cout<<" age ="<<x.age<<endl;

return 0;

OUTPUT
referencing member of structure using pointer operator
#include<iostream>

using namespace std;

struct person{

string name;

char gender;

float weight;

int age;

};

int main(){

person x;

//referencing members of structure using pointer operator

person *p;

p=&x;

cout<<"Enter the Name"<<p->name<<endl;

cin>>p->name;

cout<<"Enter the gender "<<p->gender<<endl;

cin>>p->gender;

cout<<"Enter the weight "<<p->weight<<endl;

cin>>p->weight;

cout<<"Enter the age "<<p->age<<endl;

cin>>p->age;
cout<<" Name ="<<p->name<<endl;

cout<<" gender ="<<p->gender<<endl;

cout<<"weight ="<<p->weight<<endl;

cout<<" age ="<<p->age<<endl;

return 0;

OUTPUT

ARRAY OF STRUCTURE
The program to implement array of structure
#include <iostream>

using namespace std;

// Define a structure for a student

struct Person {

string name;

int age;

char gender;

};

int main() {
// Declare an array of structures

Person W[3];

// Populate the array

for (int i = 0; i < 3; ++i) {

cout << "Enter Person " << i + 1 << " details:" << endl;

cout << "Name: ";

cin >> W[i].name;

cout << "Age: ";

cin >> W[i].age;

cout << "gender ";

cin >> W[i].gender;

// Display the details of all people

cout << "\nDetails of all people:" << endl;

for (int i = 0; i <3; ++i) {

cout << "Person " << i + 1 << ":" << endl;

cout << "Name: " << W[i].name << endl;

cout << "Age: " << W[i].age << endl;

cout << "gender: " << W[i].gender << endl;

return 0;

OUTPUT
STRUCTURE OF STRUCTURES
The program to implement the structure
#include <iostream>

using namespace std;

// Define a structure for address

struct Address {

string street;

string college;

string state;

};

// Define a structure for student

struct Student {

string name;

int age;

Address address; // Structure inside another structure

};
int main() {

// Declare a student

Student student;

// Populate student's details

cout << "Enter student's details:" << endl;

cout << "Name: ";

cin>>student.name;

cout << "Age: ";

cin >> student.age;

cout << "Address details:" << endl;

cout << "Street: ";

cin>> student.address.street;

cout << "College: ";

cin>>student.address.college;

cout << "State: ";

cin>>student.address.state;

// Display student's details

cout << "\nstudent's details:" << endl;

cout << "Name: " <<student.name << endl;

cout << "Age: " << student.age << endl;

cout << "Address:" << endl;

cout << "Street: " <<student.address.street << endl;

cout << "college: " <<student.address.college << endl;

cout << "State: " <<student.address.state << endl;


return 0;

OUTPUT

LINKED LIST
Implementation of linked list
The program to creat node

#include<iostream>

using namespace std;

struct Node{

int data;

Node*next;

};

Node*head=NULL;

int main(){

//creating first node

Node*node1=new Node();

node1->data=12;

node1->next=head;
head=node1;

//creating second node

Node*node2=new Node();

node2->data=14;

node2->next=node1->next;

node1->next=node2;

//creating third node

Node*node3=new Node();

node3->data=11;

node3->next=node2->next;

node2->next=node3;

cout<<node1->data<<endl;

cout<<node2->data<<endl;

cout<<node3->data<<endl;

return 0;

OUTPUT

The program for Deleting of a node

#include<iostream>

using namespace std;

struct steve{

int data;
steve*link;

};

steve*head;

int main(){

//creat nodes

steve*node1=new steve();

node1->data=12;

node1->link=head;

head=node1;

steve*node2=new steve();

node2->data=14;

node2->link=node1->link;

node1->link=node2;

steve*node3=new steve();

node3->data=15;

node3->link=node2->link;

node2->link=node3;

steve*node4=new steve();

node4->data=16;

node4->link=node3->link;

node3->link=node4;

if(head==NULL){

head=node4;

steve*p=head;

while(p->link!=NULL){

p=p->link;

delete (p);
p->link=NULL;

cout<<p->data<<endl;

OUTPUT

QUEUE AND STACK IMPLEMENTATIONS

The program to implement queue

#include<iostream>

using namespace std;

struct Node{

int data;

Node *next;

};

void enqueue (Node ** head, int data)

Node *node1 = new Node ();

// assign data value

node1->data = data;

// change the next node of this new_node

// to current head of Linked List

node1->next = *head;
//changing the new head to this newly entered node

*head = node1;

void dequeue (Node ** head)

Node *temp = *head;

// if there are no nodes in Linked List can't delete

if (*head == NULL)

cout << ("Linked List Empty, nothing to delete");

return;

// move head to next node

*head = (*head)->next;

//cout<< ("Deleted: %d\n", temp->data);

delete (temp);

void display (Node * node)

while (node != NULL)

cout << node->data << " ";

node = node->next;

cout << endl;

int main ()

Node *head = NULL;


enqueue (&head, 10);

enqueue (&head, 11);

enqueue (&head, 12);

enqueue (&head, 13);

enqueue (&head, 14);

enqueue (&head, 15);

enqueue (&head, 16);

enqueue (&head, 17);

enqueue (&head, 18);

cout << "Queue before deletion: ";

display (head);

dequeue (&head);

cout << endl << "Queue after deletion: ";

display (head);

return 0;

OUTPUT

The program to implement stack

#include <iostream>

using namespace std;

struct Node {
int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));

newnode->data = val;

newnode->next = top;

top = newnode;

void pop() {

if(top==NULL)

cout<<"Stack Underflow"<<endl;

else {

cout<<"The popped element is "<< top->data <<endl;

top = top->next;

void display() {

struct Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else {

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL) {

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;
}

int main() {

int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

case 3: {

display();

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;
}

}while(ch!=4);

return 0;

OUTPUT

You might also like