0% found this document useful (0 votes)
1 views18 pages

Printouts (Ds Lab)

The document contains multiple C programs demonstrating basic array and linked list operations, including adding, inserting, updating, and deleting elements in an array, as well as implementing stack and queue data structures using both arrays and linked lists. Each section includes code snippets, example outputs, and confirms successful compilation and interpretation of the programs. The programs provide practical examples of fundamental data structure manipulations in C.

Uploaded by

MARAMMA THALLI
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)
1 views18 pages

Printouts (Ds Lab)

The document contains multiple C programs demonstrating basic array and linked list operations, including adding, inserting, updating, and deleting elements in an array, as well as implementing stack and queue data structures using both arrays and linked lists. Each section includes code snippets, example outputs, and confirms successful compilation and interpretation of the programs. The programs provide practical examples of fundamental data structure manipulations in C.

Uploaded by

MARAMMA THALLI
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/ 18

ADD AN ELEMENT AT BEGINNING OF ARRAY

#include<stdio.h>
void main()
{
int a[20],n,i,item;

printf("Enter size of array : ");


scanf("%d",&n);
printf("Enter elements in array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Insert element in array at beginning");
scanf("%d",&item);
n++;
for(i=n;i>=1;i--)
{
a[i]=a[i-1];
}
a[0]=item;
printf("\nResultant array elements : ");
for(i=0;i<n;i++) {
printf("\n%d", a[i]);
}
}

OUTPUT:

Enter size of array : 3


Enter elements in array
10
20
30
Insert element in array at beginning123
Resultant array elements :
123
10
20
30

Result: The program was compiled & interpreted successfully.


INSERT AN ELEMENT AT GIVEN INDEX OF ARRAY
#include<stdio.h>
void main()
{
int a[20],n,i,index,item;
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements in array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter index you want to insert element in array ");
scanf("%d",&index);
printf("Enter item you want to insert");
scanf("%d",&item);
for(i=n;i>=index;i--)
{
a[i]=a[i-1];
}
a[index]=item;
n++;
printf("\nResultant array elements : ");
for(i=0;i<n;i++) {
printf("\n%d", a[i]);
}
}

OUTPUT:

Enter size of array : 3


Enter elements in array
11
22
33
Enter index you want to insert element in array 1
Enter item you want to insert50
Resultant array elements :
11
50
22
33
Result: The program was compiled & interpreted successfully.
UDATE AN ELEMENT AT GIVEN INDEX OF ARRAY

#include<stdio.h>
void main()
{
int a[20],n,i,indexpos,item;
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements in array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter index you want to update element in array ");
scanf("%d",&indexpos);
printf("Enter item you want to update");
scanf("%d",&item);
printf("\nResultant array after update element : ");
for(i=0;i<n;i++)
{
a[indexpos]=item;
printf("\n%d",a[i]);
}
}

OUTPUT:

Enter size of array : 3


Enter elements in array
10
20
30
Enter index you want to update element in array 2
Enter item you want to update555

Resultant array after update element :


10
20
555

Result: The program was compiled & interpreted successfully.


DELETE AN ELEMENT AT GIVEN INDEX OF ARRAY

#include<stdio.h>
void main()
{
int a[20],n,i,index,item;

printf("Enter size of array : ");


scanf("%d",&n);
printf("Enter elements in array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter index you want to delete element in array ");
scanf("%d",&index);
if(index>=n)
printf("Deletion not possible\n");
else
{
for(i=index;i<n-1;i++)
a[i]=a[i+1];
printf("\nResultant array elements : ");
for(i=0;i<n-1;i++) {
printf("\n%d", a[i]);
}
}
}

OUTPUT:

Enter size of array : 3


Enter elements in array
10
20
30
Enter index you want to delete element in array 1

Resultant array elements :


10
30

Result: The program was compiled & interpreted successfully.


IMPLEMENTATION OF STACK USING ARRAY

#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("*********Stack operations using array*********");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: show();
break;
case 4: printf("Exiting....");
break;
default: printf("Please Enter valid choice ");
};
}
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}

OUTPUT:

Enter the number of elements in the stack 2


*********Stack operations using array*********
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value?10
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value?20
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 3
20
10
Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 2
Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 3
10
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 2
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 3
Stack is empty
Chose one from the below options...
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 4
Exiting....

Result: The program was compiled & interpreted successfully.


IMPLIMENTATION OF STACK USING LINKED LIST

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Exiting....");
break;
default: printf("Please Enter valid choice ");
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped %d ",item);
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

OUTPUT:

*********Stack operations using linked list*********


----------------------------------------------
Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value100
Item pushed

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value200
Item pushed

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 3
Printing Stack elements
200
100

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 2
Item popped 200

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit
Enter your choice 4
Exiting....

Result: The program was compiled & interpreted successfully.


IMPLIMENTATION OF QUEUE USING ARRAY

#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************MainMenu*****************************\n");
printf("\n========================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nEnter valid choice??\t");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\t");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");

}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}

OUTPUT:

*************************Main Menu*****************************
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element 100
Value inserted
*************************Main Menu*****************************
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
100
************************Main Menu*****************************
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?2
value deleted
*************************Main Menu*****************************
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?4

Result: The program was compiled & interpreted successfully.


IMPLEMENTATION OF QUEUE USING LINKEDLIST

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************MainMenu****************************\n");
printf("\n========================================================\
n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

OUTPUT:

*************************Main Menu*****************************
=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1


Enter value?
100

*************************Main Menu*****************************
=================================================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
100
*************************Main Menu*****************************
=================================================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?2
*************************Main Menu*****************************
=================================================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?4

Result: The program was compiled & interpreted successfully.

You might also like