[go: up one dir, main page]

0% found this document useful (0 votes)
221 views6 pages

List Adt

The document describes implementing a list data structure using arrays in C. It includes functions to create an array-based list, insert elements in first, middle, and last positions, delete elements from first, middle, and last positions, display the list, and search for an element. The program defines functions for each list operation and uses a switch case in main() to call the desired function based on user input.

Uploaded by

5052 - UTHRA .T
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)
221 views6 pages

List Adt

The document describes implementing a list data structure using arrays in C. It includes functions to create an array-based list, insert elements in first, middle, and last positions, delete elements from first, middle, and last positions, display the list, and search for an element. The program defines functions for each list operation and uses a switch case in main() to call the desired function based on user input.

Uploaded by

5052 - UTHRA .T
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/ 6

Ex No.

1 Implementation of List ADT using Arrays

Aim:

To implement List ADT using Arrays

Description:

A list is a linear data structure, in which the elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers

Algorithm:

Step1: Start
Step 2: Create a list with size ten.
Step 3: Read a minimum of two elements.
Step 4: Stop.

 Insert First:
Step 1: Read the element to be inserted in the first position.
Step 2: Move elements in the existing list one position forward.
Step 3: Insert the element read in the first position.
Step 4: Stop.
 Insert Middle:
Step 1: Read the position and element to be inserted.
Step 2: Move elements from the required position in the existing list one position
forward.
Step 3: Insert the element read in the required position.
Step 4: Stop.
 Insert Last:
Step 1: Read the element to be inserted in the last position.
Step 2: Move the pointer variable pointing to the last element in the existing list one
position
forward.
Step 3: Insert the element read in the last position.
Step 4: Stop.
 Delete First:
Step 1: Copy the element to be deleted from the first position to a temporary
element.
Step 2: Move elements in the existing list one position backward.
Step 3: Print the element to be deleted.
Step 4: Stop.
 Delete Middle:
Step 1: Copy the element to be deleted from the required position to a temporary
element.
Step 2: Move elements in the existing list one position backward till the required
position.
Step 3: Print the element to be deleted.
Step 4: Stop.
 Delete Last:
Step 1: Copy the element to be deleted from the last position to a temporary
element.
Step 2: Decrement the top pointer by one.
Step 3: Print the element to be deleted.
Step 4: Stop.
 Find Key:
Step 1: Read the element to be searched.
Step 2: Move the top pointer through the list until element found.
Step 3: Print the element’s position.
Step 4: Stop.
 Find Position:
Step 1: Read the position
Step 2: Print the element at that position
Step 4: Stop.

Program:

#include<stdio.h>
#include<conio.h>
intarr[50],n,i;
void create();
void insert_f();
void insert_m();
void insert_l();
void delete_f();
void delete_m();
void delete_l();
void display();
void search();
void main()
{
intch;
clrscr();
while(1)
{
printf("\n ********** MENU ********** ");
printf("\n 1.Create \n 2.Insert First \n 3. Insert Middle \n 4. Insert last");
printf("\n 5. Delete First\n 6.Delete Middle\n 7.Delete Last\n");
printf(" 8.Display \n 9.Search \n 10. Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:insert_f();
break;
case 3:insert_m();
break;
case 4:insert_l();
break;
case 5:delete_f();
break;
case 6:delete_m();
break;
case 7:delete_l();
break;
case 8:display();
break;
case 9:search();
break;
case 10:exit(0);
break;
default: printf("\n Invalid entry");
}
}
}
void create()
{
printf("\n Enter the no.of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Created successfully");
printf("\n");
}
void insert_f()
{
int t;
printf("\n Enter the element");
scanf("%d",&t);
for(i=n;i>0;i--)
{
arr[i]=arr[i-1];
}
n=n++;
arr[0]=t;
printf("\n The element is inserted \n");
}
void insert_m()
{
intt,pos;
printf("\n Enter the position:");
scanf("%d",&pos);
printf("\n Enter the element:");
scanf("%d",&t);
for(i=n;i>pos-1;i--)
{
arr[i]=arr[i-1];
}
n=n++;
arr[pos-1]=t;
printf("\n The element is inserted \n");
}
void insert_l()
{
int t;
printf("\n Enter the element:");
scanf("%d",&t);
arr[n]=t;
n=n++;
printf("\n The element is inserted \n");
}
void delete_f()
{
int t;
t=arr[0];
for(i=0;i<n;i++)
{
arr[i]=arr[i+1];
}
n=n--;
printf("\n The element is deleted %d",t);
printf("\n");
}
void delete_m()
{
intpos,t;
printf("\n Enter the positon:");
scanf("%d",&pos);
for(i=pos-1;i<n;i++)
{
arr[pos-1]=t;
}
n=n--;
printf("\n The element is deleted \n");
}
void delete_l()
{
int t;
t=arr[n-1];
n=n--;
printf("The element is deleted %d",t);
}
void display()
{
printf("Displaying list");
for(i=0;i<n;i++)
{
printf("\n The elements are: %d",arr[i]);
printf("\n");
}
}
void search()
{
intt,c=0;
printf("\n Enter the element:");
scanf("%d",&t);
for(i=0;i<n;i++)
if(arr[i]==t)
{
c=1;
break;
}
if(c==1)
{
printf("\n The element found at position %d",i+1);
}
else
{
printf("\n The element is not found ");
}
}
Output:

********** MENU **********

1.Create
2.Insert First
3. Insert Middle
4. Insert last
5. Delete First
6.Delete Middle
7.Delete Last
8.Display
9.Search
10. Exit
Enter your choice:1
Enter the no.of elements:3
Enter the elements:1 2 3

Created successfully
********** MENU **********

1.Create
2.Insert First
3. Insert Middle
4. Insert last
5. Delete First
6.Delete Middle
7.Delete Last
8.Display
9.Search
10. Exit
Enter your choice:8
Displaying List:

The elements are:1


2
3

You might also like