0% found this document useful (0 votes)
82 views

CSC 2516-Rubin Timilsinadocx PDF

The document provides details about a C program that demonstrates storing and manipulating employee details using a doubly linked list (DLL) data structure. The program includes functions to create nodes, insert nodes at the beginning and end of the list, display nodes from the beginning, and delete nodes from the beginning and end. A menu is displayed to allow the user to choose these operations and create a DLL of employees, demonstrating the use of a linked list to organize variable length records.

Uploaded by

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

CSC 2516-Rubin Timilsinadocx PDF

The document provides details about a C program that demonstrates storing and manipulating employee details using a doubly linked list (DLL) data structure. The program includes functions to create nodes, insert nodes at the beginning and end of the list, display nodes from the beginning, and delete nodes from the beginning and end. A menu is displayed to allow the user to choose these operations and create a DLL of employees, demonstrating the use of a linked list to organize variable length records.

Uploaded by

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

Programme Name: BCS(Hons.

)
Course Code: (CSC 2516)
Course Name: DATA STRUCTURE AND ALGORITHM
Date of Submission: 24th August 2021

Submitted By: Submitted To:


Student Name: Rubin Timilsina Faculty Name: : PRAKASH CHANDRA PRASAD
IUKL ID: Department: LMS
Semester: 4th
Intake: September, 2019
Q.No.1 Answer>>>>>>>>>

A data structure is a specialized format for organizing, processing, retrieving and storing data. There are
several basic and advanced types of data structures, all designed to arrange data to suit a specific
purpose. Data structures make it easy for users to access and work with the data they need in
appropriate ways. Most importantly, data structures frame the organization of information so that
machines and humans can better understand it. In computer science and computer programming, a
data structure may be selected or designed to store data for the purpose of using it with
various algorithms. In some cases, the algorithm's basic operations are tightly coupled to the data
structure's design. Each data structure contains information about the data values, relationships
between the data and -- in some cases -- functions that can be applied to the data.

The importance of data structure are:

I Data structures (how to organize data) and Algorithms (how to manipulate data) are the cores of
today’s computer programming.

II DSA lays a foundation for real programming that includes OOP’s, Recursion, Sorting, Recursion
and more

III It can be an effective way to organize the memory.

The five data structures are:


a. Arrays.
b. Stacks.
c. Queues.
d. Linked Lists.
e. Trees.

Q.NO.2 Answer>>>>>>>>>>>>>>>>>

Linear Search Linear search, also called sequential search, is a very simple method used for searching an
array for a particular value. It works by comparing the value to be searched with every element of the
array one by one in a sequence until a match is found. Linear search is mostly used to search an
unordered list of elements (array in which data elements are not sorted).
For example, if an array A[] is declared and initialized as, int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5}; and the
value to be searched is VAL = 7 , then searching means to find whether the value ‘7’ is present in the
array or not. If yes, then it returns the position of its occurrence. Here, POS = 3 (index starting from 0).
Algorithm: linear search
LINEAR_SEARCH(A, N, VAL)

Step 1: [INITIALIZE] SET POS = -1

Step 2: [INITIALIZE] SET I = 1

Step 3: Repeat Step 4 while I<=N

Step 4: IF A[I] = VAL

SET POS = I

PRINT POS

Go to Step 6

[END OF IF]

SET I = I + 1

[END OF LOOP]

Step 5: IF POS = –1

PRINT " VALUE IS NOT PRESENT IN THE

ARRAY "

[END OF IF]

It’s efficiency goes as follow:

Linear search executes in O(n) time where n is the number of elements in the array. Obviously, the best
case of linear search is when VAL is equal to the first element of the array. In this case, only one
comparison will be made. Likewise, the worst case will happen when either VAL is not present in the
array or it is equal to the last element of the array. In both the cases, n comparisons will have to be
made. However, the performance of the linear search algorithm can be improved by using a sorted
array.
Q.No. 3 Answer>>>>>>>>

// C-program to demonstrate employer


// details using a Doubly-linked list

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Global declaration
int count = 0;

// Structure declaration
struct node {
struct node* prev;
int ssn;
long int phno;
float sal;
char name[20], dept[10], desg[20];
struct node* next;
} * h, *temp, *temp1, *temp2, *temp4;

// Function to create node


void create()
{
int ssn;
long int phno;
float sal;
char name[20], dept[10], desg[20];
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n enter ssn, name, depart"
"ment, designation, salary "
"and phno of employee:\n");
scanf("%d %s %s %s %f %ld",
&ssn, name, dept, desg,
&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name, name);
strcpy(temp->dept, dept);
strcpy(temp->desg, desg);
temp->sal = sal;
temp->phno = phno;
count++;
}

// Function to insert at beginning


void insertbeg()
{
// If DLL is empty
if (h == NULL) {
create();
h = temp;
temp1 = h;
}

// Else create a new node and


// update the links
else {
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}

// Function to insert at end


void insertend()
{
// If DLL is empty
if (h == NULL) {
create();
h = temp;
temp1 = h;
}

// Else create a new node and


// update the links
else {
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}

// Function to display from beginning


void displaybeg()
{
temp2 = h;
if (temp2 == NULL) {
printf("\n list is empty\n");
return;
}
printf("\n linked list elements "
"from beginning:\n");
while (temp2 != NULL) {
printf("%d %s %s %s %f %ld\n",
temp2->ssn, temp2->name,
temp2->dept, temp2->desg,
temp2->sal, temp2->phno);
temp2 = temp2->next;
}

// Print the count


printf("number of employees=%d", count);
}

// Function to delete at end


int deleteend()
{
struct node* temp;
temp = h;
if (temp == NULL) {
printf("list is empty\n");
return 0;
}
if (temp->next == NULL) {
printf("%d %s %s %s %f %ld\n",
temp->ssn, temp->name,
temp->dept, temp->desg,
temp->sal, temp->phno);
free(temp);
h = NULL;
}
else {
temp = temp1;
temp2 = temp1->prev;
temp2->next = NULL;
printf("%d %s %s %s %f %ld\n",
temp->ssn, temp->name,
temp->dept, temp->desg,
temp->sal, temp->phno);
free(temp);
temp1 = temp2;
}
count--;
return 0;
}

// Function to delete from beginning


int deletebeg()
{
struct node* temp;
temp = h;
if (temp == NULL) {
printf("list is empty\n");
return 0;
}
if (temp->next == NULL) {
printf("%d %s %s %s %f %ld\n",
temp->ssn, temp->name,
temp->dept, temp->desg,
temp->sal, temp->phno);
free(temp);
h = NULL;
}
else {
h = h->next;
h->prev = NULL;
printf("%d %s %s %s %f %ld\n",
temp->ssn, temp->name,
temp->dept, temp->desg,
temp->sal, temp->phno);
free(temp);
}
count--;
return 0;
}

// Function displaying menus


void employerDetails()
{
int ch, n, i;
h = NULL;
temp = temp1 = NULL;
printf("--------Menu------------\n");
printf("\n 1.create a DLL of n emp");
printf("\n 2.display from beginning");
printf("\n 3.insert at end");
printf("\n 4.delete at end");
printf("\n 5.insert at beginning");
printf("\n 6.delete at beginning");
printf("\n 7.to show DLL as queue");
printf("\n 8.exit\n");
printf("----------------------\n");
while (1) {
printf("\n enter choice : ");
scanf("%d", &ch);

// Switch statements begins


switch (ch) {
case 1:
printf("\n enter number of"
" employees:");
scanf("%d", &n);
for (i = 0; i < n; i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
printf(
"\n to show DLL as queue"
" \n1.perform insert and"
" deletion operation by "
"calling insertbeg() and "
"deleteend() respectvely\n "
"\t OR \n 2.perform insert "
"and delete operations by"
"calling insertend() and "
"deletebeg() respectively\n");
break;
case 8:
exit(0);
default:
printf("wrong choice\n");
}
}
}

//
int main()
{
// Function Call
employerDetails();
return 0;
}

Output:

You might also like