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

ECE- LAB PROGRAM

The document contains multiple C programs demonstrating data structures including arrays and linked lists for implementing lists, stacks, and their operations such as insertion, deletion, and searching. Each section provides a clear structure for creating, displaying, and manipulating data within these data structures. The programs are designed to be interactive, allowing user input to perform various operations.

Uploaded by

diliptrichy05
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)
3 views18 pages

ECE- LAB PROGRAM

The document contains multiple C programs demonstrating data structures including arrays and linked lists for implementing lists, stacks, and their operations such as insertion, deletion, and searching. Each section provides a clear structure for creating, displaying, and manipulating data within these data structures. The programs are designed to be interactive, allowing user input to perform various operations.

Uploaded by

diliptrichy05
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

------------------------------------------------------------------------------------------------------------------------------------------

LIST USING ARRAY


------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>

#define MAX 30

int list[MAX], n = 0;

// Function to create the list


void createList()
{
int i;
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter the elements:\n");


for(i = 0; i < n; i++)
scanf("%d", &list[i]);
}

// Function to display the list


void displayList()
{
int i;

if(n == 0)
{
printf("List is empty.\n");
return;
}

printf("List elements are: ");


for(i = 0; i < n; i++)
printf("%d ", list[i]);

printf("\n");
}

// Function to insert an element


void insertElement()
{
int pos, item, i;

if(n == MAX)
{
printf("List is full.\n");
return;
}

printf("Enter the position (1 to %d): ", n + 1);


scanf("%d", &pos);

if(pos < 1 || pos > n + 1)


{
printf("Invalid Position.\n");
return;
}

printf("Enter the element to insert: ");


scanf("%d", &item);

for(i = n; i >= pos; i--)


list[i] = list[i - 1];

list[pos - 1] = item;
n++;

printf("Element inserted successfully.\n");


}

// Function to delete an element


void deleteElement()
{
int pos, i;

if(n == 0)
{
printf("List is empty.\n");
return;
}

printf("Enter the position to delete (1 to %d): ", n);


scanf("%d", &pos);

if(pos < 1 || pos > n)


{
printf("Invalid Position.\n");
return;
}

for(i = pos - 1; i < n - 1; i++)


list[i] = list[i + 1];

n--;
printf("Element deleted successfully.\n");
}

// Function to search an element


void searchElement()
{
int item, i;

printf("Enter the element to search: ");


scanf("%d", &item);

for(i = 0; i < n; i++)


{
if(list[i] == item)
{
printf("Element found at position %d\n", i + 1);
return;
}
}

printf("Element not found.\n");


}

// Main Function
int main()
{
int choice;

createList();

for(;;)
{
printf("\n------ LIST ADT USING ARRAY ------\n");
printf("1. Display\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Search\n");
printf("5. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
displayList();
break;
case 2:
insertElement();
displayList();
break;

case 3:
deleteElement();
displayList();
break;

case 4:
searchElement();
break;

case 5:
printf("Program terminated.\n");
return(0);

default:
printf("Invalid choice.\n");
}

return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------
SINGLY LINKED LIST
------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

// Structure for a node


struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

// Function to create the linked list


void createList()
{
struct node *newNode, *temp;
int n, i;

printf("Enter the number of nodes: ");


scanf("%d", &n);

for(i = 1; i <= n; i++)


{
newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter data for node %d: ", i);


scanf("%d", &newNode->data);

newNode->next = NULL;

if(head == NULL)
{
head = newNode;
temp = head;
}
else
{
temp->next = newNode;
temp = newNode;
}
}
}

// Function to display the linked list


void displayList()
{
struct node *temp = head;

if(head == NULL)
{
printf("Linked List is Empty.\n");
return;
}

printf("\nLinked List: ");

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}

printf("NULL\n");
}

// Function to insert at the beginning


void insertFirst()
{
struct node *newNode;

newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter element to insert: ");


scanf("%d", &newNode->data);

newNode->next = head;
head = newNode;
}

// Function to insert at the end


void insertLast()
{
struct node *newNode, *temp;

newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter element to insert: ");


scanf("%d", &newNode->data);

newNode->next = NULL;

if(head == NULL)
{
head = newNode;
return;
}

temp = head;

while(temp->next != NULL)
temp = temp->next;

temp->next = newNode;
}

// Function to insert at a specified position


void insertMiddle()
{
struct node *newNode, *temp;
int pos, i;
printf("Enter the position to insert: ");
scanf("%d", &pos);

newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter the element to insert: ");


scanf("%d", &newNode->data);

// Insert at the beginning


if(pos == 1)
{
newNode->next = head;
head = newNode;
return;
}

temp = head;

// Traverse to the node before the required position


for(i = 1; i < pos - 1 && temp != NULL; i++)
{
temp = temp->next;
}

if(temp == NULL)
{
printf("Invalid Position!\n");
free(newNode);
return;
}

// Insert the new node


newNode->next = temp->next;
temp->next = newNode;
}

int main()
{
int choice;

createList();
displayList();
for(;;)
{
printf("\n------ LIST ADT USING ARRAY ------\n");
printf("1. Insert First\n");
printf("2. Insert Last\n");
printf("3. Insert Middle\n");
printf("4. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
insertFirst();
displayList();
break;

case 2:
insertLast();
displayList();
break;

case 3:
insertMiddle();
displayList();
break;

case 4:
printf("Program terminated.\n");
return 0 ;

default:
printf("Invalid choice.\n");
}

return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------
SINGLY LINKED LIST - DELETION
------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

// Create List
void create()
{
struct node *newnode, *temp;
int n, i;

printf("Enter number of nodes: ");


scanf("%d", &n);

head = NULL;

for(i = 1; i <= n; i++)


{
newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data: ");


scanf("%d", &newnode->data);

newnode->next = NULL;

if(head == NULL)
head = newnode;
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;

temp->next = newnode;
}
}

printf("List Created Successfully.\n");


}

// Display
void display()
{
struct node *temp = head;

if(head == NULL)
{
printf("List is Empty.\n");
return;
}

printf("Linked List: ");

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}

printf("NULL\n");
}

// Delete First Node


void deletefirst()
{
struct node *temp;

if(head == NULL)
{
printf("List is Empty.\n");
return;
}

temp = head;
head = head->next;

free(temp);

printf("First Node Deleted.\n");


}

// Delete Last Node


void deletelast()
{
struct node *temp, *prev;

if(head == NULL)
{
printf("List is Empty.\n");
return;
}

if(head->next == NULL)
{
free(head);
head = NULL;
printf("Last Node Deleted.\n");
return;
}

temp = head;

while(temp->next != NULL)
{
prev = temp;
temp = temp->next;
}

prev->next = NULL;
free(temp);

printf("Last Node Deleted.\n");


}

// Delete at Position
void deletemiddle()
{
struct node *temp, *prev;
int pos, i;

if(head == NULL)
{
printf("List is Empty.\n");
return;
}

printf("Enter Position to Delete: ");


scanf("%d", &pos);

if(pos == 1)
{
temp = head;
head = head->next;
free(temp);
printf("Node Deleted.\n");
return;
}

temp = head;

for(i = 1; i < pos; i++)


{
prev = temp;
temp = temp->next;

if(temp == NULL)
{
printf("Invalid Position.\n");
return;
}
}

prev->next = temp->next;
free(temp);

printf("Node Deleted Successfully.\n");


}
void search()
{
struct node *temp;
int key, position = 1, found = 0;

if(head == NULL)
{
printf("List is Empty.\n");
return;
}

printf("Enter the element to search: ");


scanf("%d", &key);

temp = head;

while(temp != NULL)
{
if(temp->data == key)
{
printf("Element %d found at position %d.\n", key, position);
found = 1;
break;
}

temp = temp->next;
position++;
}

if(found == 0)
printf("Element %d not found in the list.\n", key);
}
int main()
{
int choice;

while(1)
{
printf("\n====== Singly Linked List ======\n");
printf("1. Create List\n");
printf("2. Delete First Node\n");
printf("3. Delete Last Node\n");
printf("4. Delete at Position\n");
printf("5. Search an element\n");
printf("6. Display List\n");
printf("7. Exit\n");

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
create();
break;

case 2:
deletefirst();
break;

case 3:
deletelast();
break;

case 4:
deletemiddle();
break;

case 5:
search();
break;
case 6:
display();
break;

case 7:
printf("Program Terminated.\n");
exit(0);
default:
printf("Invalid Choice!\n");
}
}

return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------
STACK USING ARRAY
------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>

#define MAX 5

int stack[MAX];
int top = -1;

// Push operation
void push(int value)
{
if (top == MAX - 1)
{
printf("Stack Overflow\n");
return;
}

top++;
stack[top] = value;
printf("%d pushed into stack\n", value);
}

// Pop operation
void pop()
{
if (top == -1)
{
printf("Stack Underflow\n");
return;
}

printf("%d popped from stack\n", stack[top]);


top--;
}
// Display operation
void display()
{
int i;

if (top == -1)
{
printf("Stack is empty\n");
return;
}

printf("Stack elements are:\n");


for (i = top; i >= 0; i--)
{
printf("%d\n", stack[i]);
}
}

// Main function
int main()
{
int choice ,item;
for(;;)
{
printf("\n------ STACK ADT USING ARRAY ------\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter item: ");
scanf("%d",&item);
push(item);
break;

case 2:
pop();
break;

case 3:
display();
break;
case 4:
printf("Program terminated.\n");
return 0 ;

default:
printf("Invalid choice.\n");
}

return 0;

------------------------------------------------------------------------------------------------------------------------------------------
STACK USING LINKED LIST
------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node *next;
};

// Top pointer
struct Node *top = NULL;

// Push operation
void push(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

if (newNode == NULL) {
printf("Stack Overflow\n");
return;
}

newNode->data = value;
newNode->next = top;
top = newNode;

printf("%d pushed into stack\n", value);


}

// Pop operation
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}

struct Node *temp = top;


printf("%d popped from stack\n", top->data);
top = top->next;
free(temp);
}

// Display stack
void display() {
struct Node *temp = top;

if (temp == NULL) {
printf("Stack is empty\n");
return;
}

printf("Stack elements are:\n");


while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}

// Main function
int main() {

int choice ,item;


for(;;)
{
printf("\n------ STACK ADT USING LINKED LIST------\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter item: ");
scanf("%d",&item);
push(item);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
printf("Program terminated.\n");
return 0 ;

default:
printf("Invalid choice.\n");
}

return 0;

You might also like