0% found this document useful (0 votes)
15 views10 pages

Ex 7 - DSCP Lab

The document describes a C program to implement queue, stack and list abstract data types (ADTs) using linked lists. It provides the algorithms, program code and outputs for each implementation. The key steps are: 1. Initialize the linked list and display menu options. 2. Insert, delete and find elements in the list; push and pop in stack; enqueue and dequeue in queue. 3. Print the elements of the list, stack or queue. The program successfully implements the linked list, stack and queue ADTs using basic operations like insertion, deletion, traversal and printing.

Uploaded by

Dr. M.Kamarajan
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)
15 views10 pages

Ex 7 - DSCP Lab

The document describes a C program to implement queue, stack and list abstract data types (ADTs) using linked lists. It provides the algorithms, program code and outputs for each implementation. The key steps are: 1. Initialize the linked list and display menu options. 2. Insert, delete and find elements in the list; push and pop in stack; enqueue and dequeue in queue. 3. Print the elements of the list, stack or queue. The program successfully implements the linked list, stack and queue ADTs using basic operations like insertion, deletion, traversal and printing.

Uploaded by

Dr. M.Kamarajan
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/ 10

EX.

NO : 7A LINKED LIST IMPLEMENTATION OF LIST ADT


DATE :

AIM:
To write a C program for linked list implementation of list ADTs.

ALGORITHM:
1. Start the program.
2. Initialize linked list.
3. Display menu with linked list implementation operations.
4. If choice = 1, insert an element in the list.
5. If choice = 2, delete an element in the list.
6. If choice = 3, find an element in the list.
7. If choice = 4, display the elements in the list.
8. If choice = 5, exit.
9. Stop the program.

PROGRAM:
/* Linked List Implementation of List ADT */
#include <stdio.h>
#include <conio.h>
struct node
{
int element;
struct node *next;
};
typedef struct node* list;
list head = NULL, ptr = NULL;
void insert (int, int);
void del (int);
list find (int);
list findprevious (int);
void print ();
void main ()
{
list l = NULL, t = NULL;
int val, pos, choice = 0;
while (choice < 5)
{
clrscr ();
printf (“Linked List Implementation of List ADT”);
printf (“\n1.Insert”);
printf (“\n2.Delete”);
printf (“\n3.Find”);
printf (“\n4. Display”);
printf (“\n5.Exit”);

1
printf (“\nEnter your choice: ”);
scanf (“%d”, &choice);
switch (choice)
{
case 1: printf (“Enter position and element: ”);
scanf (“%d %d”, &pos, &val);
insert (pos, val);
break;
case 2: printf (“Enter element to delete: ”);
scanf (“%d”, &val);
del (val);
break;
case 3: printf (“Enter element to find: ”);
scanf (“%d”, &val);
t = find (val);
if (t != NULL)
printf (“Element found %d”, t->element);
else
printf (“Element not found.”);
getch ();
break;
case 4: print (ptr);
getch ();
break;
}
}
}
void insert (int pos, int v)
{
list newnode;
int i;
ptr = head->next;
newnode = malloc (sizeof (struct node));
newnode->element = v;
newnode->next = NULL;
if (pos == 1)
{
newnode->next = head->next;
head->next = newnode;
}
else
{
for (i = 1; i < pos-1 && ptr->next != NULL; i++)
{
ptr = ptr->next;
}

2
newnode->next = ptr->next;
ptr->next = newnode;
}
}
void del (int v)
{
list fp;
ptr = head->next;
if (ptr->element == v)
{
head->next = head->next->next;
free (ptr);
}
else
{
fp = findprevious (v);
if (fp != NULL)
{
ptr = fp->next;
fp->next = fp->next->next;
free (ptr);
}
}
}
list find (int v)
{
ptr = head->next;
while (ptr != NULL)
{
if (ptr->element == v)
return ptr;
ptr = ptr->next;
}
return NULL;
}
list findprevious(int v)
{
list fp = head;
ptr = head->next;
while (ptr != NULL)
{
if (ptr->element == v)
return fp;
fp = ptr;
ptr = ptr->next;

3
}
return NULL;
}
void print ()
{
ptr = head->next;
while (ptr != NULL)
{
printf (“%d->”, ptr->element);
ptr = ptr->next;
}
}

OUTPUT:
Linked List Implementation of List ADT
1. Insert
2. Delete
3. Find
4. Display
5. Exit
Enter your choice: 1
Enter position and element: 1 10
Enter your choice: 1
Enter position and element: 2 20
Enter your choice: 1
Enter position and element: 3 30
Enter your choice: 4
10->20->30->
Enter your choice: 3
Enter element to find: 20
Element found 20
Enter your choice: 2
Enter element to delete: 20
Enter your choice: 4
10->30->
Enter your choice: 5

RESULT:
Thus, the linked list implementation of list ADT was implemented
successfully.

4
EX. NO : 7B LINKED LIST IMPLEMENTATION OF STACK ADT
DATE :

AIM:
To write a C program for linked list implementation of stack ADTs.

ALGORITHM:
1. Start the program.
2. Initialize linked list as stack.
3. Display menu with linked list implementation operations.
4. If choice = 1, push an element in the stack.
5. If choice = 2, pop an element in the stack.
6. If choice = 3, display the elements in the stack.
7. If choice = 5, exit.
8. Stop the program.

PROGRAM:
/* Linked List Implementation of Stack ADT */
#include <stdio.h>
#include <conio.h>
struct node
{
int element;
struct node *next;
};
typedef struct node *list;
list head = NULL, ptr = NULL;
void push (int);
list pop ();
void print ();
void main ()
{
int val, choice = 0;
while (choice < 4)
{
clrscr ();
printf (“Linked List Implementation of Stack ADT”);
printf (“\n1. Push \n2. Pop \n3. Display \n4. Exit\n”);
printf (“\nEnter your choice: ”);
scanf (“%d”, &choice);
switch (choice)
{
case 1:printf (“Enter the element: ”);
scanf (“%d”, &val);
push (val);
break;

5
case 2:ptr = pop ();
if (ptr != NULL)
{
printf (“The popped element %d.”, ptr->element);
free (ptr);
}
getch ();
break;
case 3:print ();
getch ();
break;
}
}
}
void push (int v)
{
list newnode;
int i;
newnode = malloc (sizeof( struct node));
newnode->element = v;
newnode->next = head->next;
head->next = newnode;
}
list pop ()
{
if (head->next == NULL)
{
printf (“Stack Empty”);
return NULL;
}
else
{
ptr = head->next;
head->next = head->next->next;
return (ptr);
}
}
void print ()
{
ptr = head->next;
while (ptr != NULL)
{
printf (“%d->”, ptr->element);
ptr = ptr->next;
}

6
}

OUTPUT:
Linked List Implementation of Stack ADT
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the element: 10
Enter your choice: 1
Enter the element: 20
Enter your choice: 1
Enter the element: 30
Enter your choice: 3
30->20->10->
Enter your choice: 2
The popped element is 30.
Enter your choice: 3
20->10->
Enter your choice: 4

RESULT:
Thus, the linked list implementation of stack ADT was implemented
successfully.

7
EX. NO : 7C LINKED LIST IMPLEMENTATION OF QUEUE ADT
DATE :

AIM:
To write a C program for linked list implementation of queue ADTs.

ALGORITHM:
1. Start the program.
2. Initialize queue.
3. Display menu with linked list implementation operations.
4. If choice = 1, enqueue an element in the queue.
5. If choice = 2, dequeue an element in the queue.
6. If choice = 3, display the elements in the queue.
7. If choice = 5, exit.
8. Stop the program.

PROGRAM:
/* Linked List Implementation of Queue ADT */
#include <stdio.h>
#include <conio.h>
struct node
{
int element;
struct node *next;
};
typedef struct node *list;
list head = NULL, ptr = NULL;
void enqueue (int);
list dequeue ();
void print ();

int main ()
{
int choice = 0, p, x;
while (choice <= 3)
{
clrscr ();
printf (“Linked List Implementation of Queue ADT”);
printf (“1. Enqueue \n2. Dequeue \n3. Display \n4. Exit\n”);
printf (“\nEnter your choice: ”);
scanf (“%d”, &choice);
switch (choice)
{
case 1: printf (“Enter the value: ”);
scanf (“%d”, &x);
enqueue (x);
break;

8
case 2: ptr = dequeue ();
if (ptr == NULL)
printf (“Queue Empty”);
else
{
printf (“The dequeued element %d.”, ptr->element);
free (ptr);
}
break;
case 3: print ();
break;
}
getch ();
}
}

void enqueue (int v)


{
list newnode;
ptr = head->next;
newnode = malloc (sizeof (struct node));
newnode->element = v;
newnode->next = NULL;
if (head->next == NULL)
head->next = newnode;
else
{
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = newnode;
}
}

list dequeue ()
{
if (head->next != NULL)
{
ptr = head->next;
head->next = head->next->next;
return ptr;
}
else
return NULL;
}

void print ()
{

9
ptr = head->next;
while (ptr != NULL)
{
printf (“%d->”, ptr->element);
ptr = ptr->next;
}
}

OUTPUT:
Linked List Implementation of Queue ADT
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value: 10
Enter your choice: 1
Enter the value: 20
Enter your choice: 1
Enter the value: 30
Enter your choice: 3
10->20->30->
Enter your choice: 2
The dequeued element is 10.
Enter your choice: 3
20->30->
Enter your choice: 4

RESULT:
Thus, the linked list implementation of queue ADT was implemented
successfully.

10

You might also like