ds lab manual[1]

99
Ex: No: 1 IMPLEMENTATION OF SINGLY LINKED LIST AIM: To write a program to create insert, delete, modify, and view nodes in a singly linked list. ALGORITHM: 1. Start the program execution. 2. The basic operation performed are a. Creation of a list b. Insertion of a node c. Modification of a node d. Deletion of a node e. View the node 3. In creation of a list a. Initialize the top pointer by –1. b. Read the data to be store in the list roll, by incrementing the top pointer by 1. 4. In insertion of a node a. Check whether the list is fully occupied or not. b. If the list is full, insert operation is terminated. If the list is not full, follow the next steps position. c. Increment the top pointer by 1 d. Read the data to be stored in the first position of the list roll. 5. In modification of a node a. Check whether the list is empty or not b. If the list is empty, the modification operation terminates. c. Change the information of the node.

Upload: sathyaaaaa1

Post on 04-Mar-2015

300 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DS Lab Manual[1]

Ex: No: 1 IMPLEMENTATION OF SINGLY LINKED LIST

AIM:

To write a program to create insert, delete, modify, and view nodes in a singly linked list.

ALGORITHM:

1. Start the program execution.

2. The basic operation performed area. Creation of a listb. Insertion of a nodec. Modification of a noded. Deletion of a nodee. View the node

3. In creation of a list a. Initialize the top pointer by –1.b. Read the data to be store in the list roll, by incrementing the top pointer by 1.

4. In insertion of a nodea. Check whether the list is fully occupied or not.b. If the list is full, insert operation is terminated. If the list is not full, follow the

next steps position.c. Increment the top pointer by 1d. Read the data to be stored in the first position of the list roll.

5. In modification of a node a. Check whether the list is empty or notb. If the list is empty, the modification operation terminates.c. Change the information of the node.

6. In deletion of a nodea. Check whether the list is empty or not.b. Left shift the existing data in the list roll by one, from its second position to the

last position.c. Decrement the top pointer by 1.

7. Display the result.

8. Stop the program execution.

Page 2: DS Lab Manual[1]

PROGRAM:

/* SINGLY LINKED LIST */

#include<stdio.h>

#include<conio.h>

typedef struct list

{

int roll;

char name[20];

struct list *link;

}node;

node* getnode();

void createlist(node **headptr);

void insertfirst(node **headptr);

void insertlast(node **headptr);

void insertmiddle(node **headptr);

void deletefirst(node **headptr);

void deletelast(node **headptr);

void deletemiddle(node **headptr);

void modifynode(node *head);

void viewlist(node *head);

int countlist(node *head);

void releasenode(node *newnode);

void displaymenu();

void main()

{

node *head = NULL;

int ch,count;

displaymenu();

while(1)

{

2

Page 3: DS Lab Manual[1]

printf("\nEnter your choice\a:");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

{

case 0 :

createlist(&head);

break;

case 1 :

insertfirst(&head);

break;

case 2 :

insertlast(&head);

break;

case 3 :

insertmiddle(&head);

break;

case 4 :

deletefirst(&head);

break;

case 5 :

deletelast(&head);

break;

case 6 :

deletemiddle(&head);

break;

case 7 :

modifynode(head);

break;

case 8 :

viewlist(head);

3

Page 4: DS Lab Manual[1]

break;

case 9 :

count = countlist(head);

printf("Num,ber of nodes in the list is %d",count);

break;

case 10 :

displaymenu();

break;

default :

printf("End of run of your program");

exit(0);

}

}

}

void displaymenu()

{

clrscr();

printf("\nBasic operations in a Singly Linked List");

printf("\n\t 0. Creatlist");

printf("\n\t 1. Insert First");

printf("\n\t 2. Insert Last");

printf("\n\t 3. Insert Middle");

printf("\n\t 4. Delete First");

printf("\n\t 5. Delete Last");

printf("\n\t 6. Delete Middle");

printf("\n\t 7. Modify Node");

printf("\n\t 8. View List");

printf("\n\t 9. Count Nodes");

printf("\n\t 10. Show Menu");

printf("\n\t 11. Exit");

}

4

Page 5: DS Lab Manual[1]

node* getnode()

{

node * newnode;

int size = sizeof(node);

newnode = (node *)malloc(size);

return(newnode);

}

void readnode(node* newnode)

{

printf("\nEnter The RollNo:");

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

printf("\nEnter the Name:");

scanf("%s",newnode->name);

newnode->link = NULL;

}

void releasenode(node *newnode)

{

free(newnode);

}

void createlist(node **headptr)

{

node *head = NULL,*newnode,*last;

char ch;

do

{

newnode = getnode();

readnode(newnode);

5

Page 6: DS Lab Manual[1]

if(head == NULL)

{

head = newnode;

last = head;

}

else

{

last->link = newnode;

last = last->link;

}

fflush(stdin);

printf("Do u wish to add data in the list(Y/N)?");

scanf("%c",&ch);

}while((ch == 'Y')||(ch == 'y'));

*headptr = head;

displaymenu();

}

void insertfirst(node **headptr)

{

node *head,*newnode;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

newnode->link = head;

head = newnode;

}

6

Page 7: DS Lab Manual[1]

*headptr = head;

displaymenu();

}

void insertlast(node **headptr)

{

node *head,*last,*newnode;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

last=head;

while(last->link != NULL)

last = last->link;

last->link = newnode;

}

*headptr = head;

displaymenu();

}

void insertmiddle(node **headptr)

{

node *head,*last,*newnode;

int insdata;

head = *headptr;

if(head == NULL)

{

printf("Singly Linked List is Empty,so new node is headnode");

7

Page 8: DS Lab Manual[1]

newnode = getnode();

readnode(newnode);

head = newnode;

}

else

{

printf("Enter the rollno after which the insertion to be made");

scanf("%d",&insdata);

last = head;

while(last != NULL)

{

if(last->roll == insdata)

{

newnode = getnode();

readnode(newnode);

newnode->link = last->link;

last->link = newnode;

return;

}

else

last = last->link;

}

printf("Insert node is not found");

}

*headptr = head;

displaymenu();

}

void deletefirst(node **headptr)

{

node *head,*delnode;

8

Page 9: DS Lab Manual[1]

head = *headptr;

if(head == NULL)

{

printf("singly Linked List is Empty");

return;

}

delnode = head;

head = head->link;

printf("\n Deleted Roll no %d \n Name %s",delnode->roll,delnode->name);

releasenode(delnode);

*headptr = head;

}

void deletelast(node ** headptr)

{

node *head,*delnode,*last,*prev;

head = *headptr;

if(head == NULL)

{

printf("\n List is Empty");

return;

}

else if(head->link == NULL)

{

delnode = head;

head = NULL;

}

else

{

last = head;

while(last->link !=NULL)

9

Page 10: DS Lab Manual[1]

{

prev = last;

last = last->link;

}

delnode= last;

prev->link = NULL;

}

printf("\nDelted rollno is %d Name %s",delnode->roll,delnode->name);

releasenode(delnode);

*headptr = head;

}

void deletemiddle(node **headptr)

{

node *head,*delnode,*last,*prev;

int deldata;

head = *headptr;

if(head == NULL)

{

printf("\n List is Empty");

return;

}

printf("Enter the roll no to be deleted");

scanf("%d",&deldata);

if(head->roll == deldata)

{

delnode = head;

head = head->link;

printf("Deleted data \nRollno %d \n Name%s",delnode->roll,delnode->name);

releasenode(delnode);

*headptr = head;

10

Page 11: DS Lab Manual[1]

return;

}

last = head->link;

prev = head;

while(last != NULL)

{

if(last ->roll == deldata)

{

delnode = last;

prev->link = last->link;

printf("Deleted data \n Roll no %d and \t Name %s",delnode->roll,delnode->name);

releasenode(delnode);

return;

}

else

{

last = last->link;

prev = prev->link;

}

}

printf("\ndelete node is not found");

*headptr = head;

}

void modifynode(node *head)

{

int moddata;

if(head == NULL)

{

printf("\n List is Empty");

return;

11

Page 12: DS Lab Manual[1]

}

printf("\nEnter the node rollno for modification");

scanf("%d",&moddata);

while(head != NULL)

{

if(head->roll == moddata)

{

printf("\n Modify the data of the node");

printf("\nEnter the Roll no:");

scanf("%d",&head->roll);

printf("\n Enter the new Name:");

scanf("%s",head->name);

return;

}

else

head = head->link;

}

printf("\n Modify node not found");

}

void viewlist(node *head)

{

if(head == NULL)

{

printf("\NLIST IS EMPTY");

return;

}

for(;head != NULL;head = head->link)

printf("\nRollNo:%d\tName:%s",head->roll,head->name);

}

12

Page 13: DS Lab Manual[1]

int countlist(node *head)

{

int count=0;

if(head == NULL)

{

printf("\nList is Empty");

return 0;

}

for(;head != NULL;head = head->link)

{

count = count + 1;

}

return count;

}

13

Page 14: DS Lab Manual[1]

RESULT:

Thus the program for singly linked list was executed successfully.

14

Page 15: DS Lab Manual[1]

Ex. No. 2 IMPLEMENTATION OF DOUBLY LINKED LIST

AIM:

To write a program to create insert, delete, modify, and view nodes in a doubly linked list.

CONCEPTS:

1. Start the program execution.

2. The basic operation performed area. Creation of a listb. Insertion of a nodec. Modification of a noded. Deletion of a nodee. View the node

3. In creation of a list a. Initialize the top pointer by –1.b. Read the data from the userc. Connect the nodes with the list.

4. In insertion of a nodea. Get the new node using GETNODE () and read the details of the node using

READNODE.b. Check whether the list is fully occupied or not.c. If the list is full, insert operation is terminated. If the list is not full, follow the

next steps position.d. The forward link is made to point the first node e. The backward link of the first node is made to point the new node by assigning

the address of the new node.f. Assign new node as the head pointer.

5. In modification of a node a. Check whether the list is empty or notb. If the list is empty, the modification operation terminates.c. Change the information of the node.

6. In deletion of a nodea. Check whether the list is empty or not.b. Set the head pointer to the second node in the list.c. Set the backward link field of the head node in the list to NULL.

15

Page 16: DS Lab Manual[1]

d. Release the memory for the deleted node.

7. Display the result.

8. Stop the program execution.

16

Page 17: DS Lab Manual[1]

PROGRAM:

/* DOUBLY LINKED LIST */

#include<stdio.h>

typedef struct list

{

int roll;

char name[20];

struct list *flink,*blink;

}node;

clrscr();

node* getnode();

void createlist(node **headptr);

void insertfirst(node **headptr);

void insertlast(node **headptr);

void insertmiddle(node **headptr);

void deletefirst(node **headptr);

void deletelast(node **headptr);

void deletemiddle(node **headptr);

void modifynode(node *head);

void viewlist(node *head);

int countlist(node *head);

void releasenode(node *newnode);

void displaymenu();

void main()

{

node *head = NULL;

int ch,count;

displaymenu();

while(1)

{

17

Page 18: DS Lab Manual[1]

printf("\n\n ?");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

{

case 0 :

createlist(&head);

break;

case 1 :

insertfirst(&head);

break;

case 2 :

insertlast(&head);

break;

case 3 :

insertmiddle(&head);

break;

case 4 :

deletefirst(&head);

break;

case 5 :

deletelast(&head);

break;

case 6 :

deletemiddle(&head);

break;

case 7 :

modifynode(head);

break;

case 8 :

viewlist(head);

18

Page 19: DS Lab Manual[1]

break;

case 9 :

count = countlist(head);

printf("Number of nodes in the list is %d",count);

break;

case 10 :

displaymenu();

break;

default :

printf("End of run of your program");

exit(0);

clrscr();

}

}

}

void displaymenu()

{

printf("\nBasic operations in a Doubly Linked List");

printf("\n\t 0. Creatlist");

printf("\n\t 1. Insert First");

printf("\n\t 2. Insert Last");

printf("\n\t 3. Insert Middle");

printf("\n\t 4. Delete First");

printf("\n\t 5. Delete Last");

printf("\n\t 6. Delete Middle");

printf("\n\t 7. Modify Node");

printf("\n\t 8. View List");

printf("\n\t 9. Count Nodes");

printf("\n\t 10. Show Menu");

printf("\n\t 11. Exit");

19

Page 20: DS Lab Manual[1]

}

node* getnode()

{

int size;

node * newnode;

size = sizeof(node);

newnode = (node *)malloc(size);

return(newnode);

}

void readnode(node* newnode)

{

printf("\nEnter The RollNo:");

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

printf("\nEnter the Name:");

scanf("%s",newnode->name);

newnode->flink = NULL;

newnode->blink = NULL;

}

void releasenode(node *newnode)

{

free(newnode);

}

void createlist(node **headptr)

{

node *head = NULL,*newnode,*last;

char ch;

do

{

newnode = getnode();

readnode(newnode);

if(head == NULL)

20

Page 21: DS Lab Manual[1]

{

head = newnode;

last = head;

}

else

{ last->blink = last;

last->flink = newnode;

last = last->flink;

}

fflush(stdin);

printf("Do u wish to add data in the list(Y/N)?");

scanf("%c",&ch);

}

while((ch == 'Y')||(ch == 'y'));

*headptr = head;

}

void insertfirst(node **headptr)

{

node *head,*newnode;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

newnode->flink = head;

head->blink = newnode;

head = newnode;

}

*headptr = head;

21

Page 22: DS Lab Manual[1]

}

void insertlast(node **headptr)

{

node *head,*last,*newnode;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

last=head;

while(last->flink != NULL)

last = last->flink;

newnode-> blink = last;

last->flink = newnode;

}

*headptr = head;

}

void insertmiddle(node **headptr)

{

node *head,*last,*newnode,*next;

int insdata;

head = *headptr;

if(head == NULL)

{

printf("Doubly Linked List is Empty,so new node is headnode");

newnode = getnode();

readnode(newnode);

head = newnode;

}

22

Page 23: DS Lab Manual[1]

else

{

printf(" Enter the rollno after which the insertion to be made");

scanf("%d",&insdata);

last = head;

while(last != NULL)

{

if(last->roll == insdata)

{

newnode = getnode();

readnode(newnode);

next=last->flink;

newnode->flink =next;

newnode->blink =last;

last->flink = newnode;

if(next !=NULL)

next ->blink = newnode;

return;

}

else

last = last->flink;

}

printf("Insert node is not found");

}

*headptr = head;

}

void deletefirst(node **headptr)

{

node *head,*delnode;

head = *headptr;

if(head==NULL)

23

Page 24: DS Lab Manual[1]

{

printf("\n Doubly Linked List is Empty");

return;

}

delnode = head;

head = head->flink;

if(head!=NULL)

head ->blink = NULL;

printf("\n Deleted Data is.........\n");

printf("\n Roll No.: %d", delnode -> roll);

printf("\n Name: %s",delnode -> name);

releasenode(delnode);

*headptr = head;

}

void deletelast(node ** headptr)

{

node *head,*delnode,*last,*prev;

head = *headptr;

if(head == NULL)

{

printf("\n List is Empty");

return;

}

else if(head->flink == NULL)

{

delnode = head;

head = NULL;

}

else

{

last = head;

24

Page 25: DS Lab Manual[1]

while(last->flink !=NULL)

last = last-> flink;

prev = last -> blink;

delnode= last;

prev->flink = NULL;

}

printf("\nDelted rollno is %d Name %s",delnode->roll,delnode->name);

releasenode(delnode);

*headptr = head;

}

void deletemiddle(node **headptr)

{

node *head,*delnode,*last,*prev,*next;

int deldata;

head = *headptr;

if(head == NULL)

{

printf("\n List is Empty");

return;

}

printf("Enter the roll no to be deleted");

scanf("%d",&deldata);

if(head->roll == deldata)

{

delnode = head;

head = head->flink;

if(head !=NULL)

head -> blink = NULL;

printf("Deleted data \nRollno %d \n Name%s",delnode->roll,delnode->name);

releasenode(delnode);

*headptr = head;

25

Page 26: DS Lab Manual[1]

return;

}

last = head->flink;

// prev = head;

while(last != NULL)

{

if(last ->roll == deldata)

{

delnode = last;

prev = last -> blink;

next = last -> flink;

prev->flink = next;

if(next !=NULL)

next -> blink = prev;

printf("Deleted data \n Roll no %d and \t Name %s",delnode-

>roll,delnode->name);

releasenode(delnode);

return;

}

else

{

last = last->flink;

//prev = prev->link;

}

}

printf("\ndelete node is not found");

*headptr = head;

}

void modifynode(node *head)

{

int moddata;

26

Page 27: DS Lab Manual[1]

if(head == NULL)

{

printf("\n List is Empty");

return;

}

printf("\nEnter the node rollno for modification");

scanf("%d",&moddata);

while(head != NULL)

{

if(head->roll == moddata)

{

printf("\n Modify the data of the node");

printf("\nEnter the Roll no:");

scanf("%d",&head->roll);

printf("\n Enter the new Name:");

scanf("%s",head->name);

return;

}

else

head = head->flink;

}

printf("\n Modify node not found");

}

void viewlist(node *head)

{

if(head == NULL)

{

printf("\NLIST IS EMPTY");

return;

}

27

Page 28: DS Lab Manual[1]

for(;head != NULL;head = head->flink)

{

printf("\nRollNo:%d\tName:%s",head->roll,head->name);

}

}

int countlist(node *head)

{

int count=0;

if(head == NULL)

{

printf("\nList is Empty");

return count;

}

for(;head != NULL;head = head->flink)

{

count = count + 1;

}

return count;

}

28

Page 29: DS Lab Manual[1]

RESULT:

Thus the program for doubly linked list was executed successfully.

29

Page 30: DS Lab Manual[1]

Ex. No. 3 IMPLEMENTATION OF CIRCULAR LINKED LIST

AIM:

To write a program to create insert, delete, modify, and view nodes in a circular linked list.

CONCEPTS:

1. Start the program execution.2.3. The basic operation performed are

a. Creation of a listb. Insertion of a nodec. Modification of a noded. Deletion of a nodee. View the node

4. In creation of a list a. Initialize the top pointer by –1.b. Read the data from the userc. Connect the nodes with the list.

5. In insertion of a nodea. Get the new node using GETNODE() and read the details of the node using

READNODE.b. Check whether the list is fully occupied or not.c. The link field of the new node is made to point the data field of the first node in

the list by assigning the address of the first node.d. The head pointer is made to point the data field of the new node by assigning

the address of the new node.

6. In modification of a node a. Check whether the list is empty or notb. If the list is empty, the modification operation terminates.c. Change the information part of the node.

7. In deletion of a nodea. Check whether the list is empty or not.b. Set the head pointer to the second node in the list.c. Release the memory for the deleted node.

8. Display the result

9. Stop the program execution.

30

Page 31: DS Lab Manual[1]

PROGRAM:

/*Circular Linked List*/

#include <stdio.h>

typedef struct list

{

int roll;

char name[20];

struct list *link;

}node;

node* getnode();

void createlist(node **headptr);

void insertfirst(node **headptr);

void insertlast(node **headptr);

void insertmiddle(node **headptr);

void deletefirst(node **headptr);

void deletelast(node **headptr);

void deletemiddle(node **headptr);

void modifynode(node *head);

void viewlist(node *head);

void releasenode(node *newnode);

void displaymenu();

void main()

{

node *head = NULL;

int ch, count;

clrscr();

displaymenu();

while(1)

{

31

Page 32: DS Lab Manual[1]

printf("\n\n?");

fflush(stdin);

scanf("%d", &ch);

switch(ch)

{

case 0:

createlist(&head);

break;

case 1:

insertfirst(&head);

break;

case 2:

insertlast(&head);

break;

case 3:

insertmiddle(&head);

break;

case 4:

deletefirst(&head);

break;

case 5:

deletelast(&head);

break;

case 6:

deletemiddle(&head);

break;

case 7:

modifynode(head);

32

Page 33: DS Lab Manual[1]

break;

case 8:

viewlist(head);

break;

case 9:

count = countlist(head);

printf("Number of node in the list is %d", count);

break;

case 10:

displaymenu();

break;

default:

printf("End of the run of your program...");

exit(0);

}

}

}

void displaymenu()

{

printf("\n Basic operations in a circularly linked list...");

printf("\n0.Create list");

printf("\n1.insert first");

printf("\n2.insert last");

printf("\n3.insert middle");

printf("\n4.delete first");

printf("\n5.delete last");

printf("\n6.delete middle");

printf("\n7.modify node");

printf("\n8.view list");

33

Page 34: DS Lab Manual[1]

printf("\n9.count list");

printf("\n10.show menu");

printf("\n11.exit");

}

node* getnode()

{

int size;

node *newnode;

size = sizeof(node);

newnode = (node *)malloc(size);

return (newnode);

}

void readnode(node* newnode)

{

printf("\nenter the roll number:");

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

printf("Enter the name:");

scanf("%s", newnode -> name);

newnode -> link = newnode;

}

void releasenode(node *newnode)

{

free(newnode);

}

void createlist(node **headptr)

{

node * head = NULL, *newnode, *last;

char ch;

do

{

34

Page 35: DS Lab Manual[1]

newnode = getnode();

readnode(newnode);

if(head == NULL)

{

head = newnode;

last =head;

}

else

{

last -> link = newnode;

newnode -> link = head;

last= last -> link;

}

fflush(stdin);

printf("do u wish to add data in the list(y/n)? ");

scanf("%c", &ch);

}while((ch == 'Y') || (ch == 'y'));

*headptr = head;

}

void insertfirst(node **headptr)

{

node *head, *newnode, *last;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

last = head;

35

Page 36: DS Lab Manual[1]

while(last -> link != head)

last = last -> link;

last -> link = newnode;

newnode -> link = head;

head = newnode;

}

*headptr = head;

}

void insertlast(node **headptr)

{

node *head, *newnode, *last;

head = *headptr;

newnode = getnode();

readnode(newnode);

if(head == NULL)

head = newnode;

else

{

last = head;

while(last -> link != head) last = last -> link;

last -> link = newnode;

newnode -> link = head;

}

*headptr = head;

}

void insertmiddle(node **headptr)

{

node *head, *newnode, * last;

int insdata;

head = *headptr;

if(head == NULL)

36

Page 37: DS Lab Manual[1]

{

printf("The circularly linked list is empty."

"so newnode is head node.");

newnode = getnode();

readnode(newnode);

head = newnode;

}

else

{

printf("\n Enter the node roll no after which the"

"insertion is to be made:..");

scanf("%d",&insdata);

last = head;

do

{

if(last -> roll == insdata)

{

newnode =getnode();

readnode(newnode);

newnode -> link = last -> link;

last -> link = newnode;

return;

}

else

last = last -> link;

}while(last != head);

printf("The insert node is not found");

}

*headptr = head;

}

37

Page 38: DS Lab Manual[1]

void deletefirst(node **headptr)

{

node *head, *delnode, *last;

head = *headptr;

if(head == NULL)

{

printf("\n Circularly linked list is empty..");

return;

}

delnode =head;

if(head -> link == head)

{

head = NULL;

}

else

{

last = head;

while(last -> link != head);

last = last -> link;

head = head -> link;

last -> link = head;

}

printf("\n deleted data is...\n"

"\n roll number : %d",delnode -> roll);

printf("\n Name : %s", delnode -> name);

releasenode(delnode);

*headptr = head;

}

void deletelast(node **headptr)

{

node *head, *delnode, *last, *prev;

38

Page 39: DS Lab Manual[1]

head = *headptr;

if(head == NULL)

{

printf("\n circularly linked list is empty..");

return;

}

else if(head -> link == head)

{

delnode = head;

head = NULL;

}

else

{

last = head;

while(last -> link != head)

{

prev = last;

last = last -> link;

}

delnode = last;

prev -> link = head;

}

printf("\n deleted data is....\n"

"\n roll number: %d", delnode -> roll);

printf("\n name: %s", delnode -> name);

releasenode(delnode);

*headptr = head;

}

void deletemiddle(node **headptr)

{ node *head, *delnode, *last, *prev;

39

Page 40: DS Lab Manual[1]

int deldata;

head = *headptr;

if(head == NULL)

{

printf("\n circulary linked list is empty...");

return;

}

printf("Enter the node roll no for deletion is to be made:");

scanf("%d",&deldata);

last = head;

if(head -> roll == deldata)

{

delnode = head;

if(head -> link == head)

head = NULL;

else

{

last = head;

while(last -> link !=head)

{

last = last -> link;

head = head -> link;

last -> link = head;

}

}

}

else

{

prev = head;

last = head -> link;

while( last != head)

40

Page 41: DS Lab Manual[1]

{

if(last -> roll == deldata)

{

delnode = last;

prev -> link = last -> link;

break;

}

prev = prev -> link;

last = last -> link;

}

if(last == head)

{

printf("deleted node is not found");

return;

}

}

printf("\nDeleted data is ...\n");

printf("\n roll number: %d", delnode -> roll);

printf("\n name : %s", delnode -> name);

releasenode(delnode);

*headptr = head;

return;

}

void modifynode(node *head)

{

node *last;

int moddata;

if(head == NULL)

41

Page 42: DS Lab Manual[1]

{

printf("\ncircularly linked List is Empty");

return;

}

printf("\nEnter the node rollno for modification:");

scanf("%d",&moddata);

last = head;

do

{

if(last -> roll == moddata)

{

printf("\n enter the data of the node:");

printf("\nEnter the new Roll no:");

scanf("%d",&last -> roll);

printf("\n Enter the new Name:");

scanf("%s",last -> name);

return;

}

else

last = last -> link;

}while(last != head);

printf("\n Modify node not found");

}

void viewlist(node *head)

{

node *last;

if(head == NULL)

{

printf("\n circularly linked list is empty");

42

Page 43: DS Lab Manual[1]

return;

}

last = head;

for(;last != head;last = last -> link)

{

printf("\nRollNo:%d\tName:%s",head->roll,head->name);

}

}

int countlist(node *head)

{

int count = 0;

node *last;

if(head == NULL)

{

printf("\nCircularly linked List is Empty..");

return count;

}

for(;head != NULL;head = head -> link)

{

count = count + 1;

}

return count;

}

RESULT:Thus the program for doubly linked list was executed successfully.

43

Page 44: DS Lab Manual[1]

44

Page 45: DS Lab Manual[1]

Ex. No. 4 IMPLEMENTATION OF STACK USING ARRAYS

AIM:

To write a program to create, insert, delete and view nodes in the stack-using array.

CONCEPTS:

1. Start the program execution.2. The basic operation performed are

a. Creation of stackb. Push operationc. Pop operationd. Display the stack

3. In creation of a stack a. An array to hold the elements of the stack, which can be of any data types.b. The integer variable to indicate the top position of the stack within array.

4. In push operation a. Create a function push() with one argument, the element to be added.b. Assign the new element in the array by incrementing the TOP variable by 1.

5. In pop operation a. Create a function POP() with one argument, the address of the element to store

the popped value.b. Decrement the TOP variable by 1.

6. Display the result

7. Stop the program execution.

45

Page 46: DS Lab Manual[1]

PROGRAM:

RESULT:Thus the program for stack using array was executed successfully.

46

Page 47: DS Lab Manual[1]

Ex. No. 5 IMPLEMENTATION OF STACK USING LINKED LIST

AIM:

To write a program to create, insert, delete and view nodes in the stack-using linked list.

CONCEPTS:

1. Start the program execution.

2. The basic operation performed isa. Creation of stackb. Push operationc. Pop operationd. Display the stack.

3. In creation of a stack e. An array to hold the elements of the stack, which can be of any data types.f. The integer variable to indicate the top position of the stack within array.

4. In push operation a. Create a function push () with two argument.b. Create a new pointer to hold the new element. c. Allocate size of the stack node to the new pointer using GETNODE()d. The size of the stack depends on the heap memory is not available.e. Assign the value to the data of new pointer.

5. In pop operation a. Create a function POP() with two argument, the address of the element to store

the popped value.b. Create a temporary pointer to hold the removed element.c. Assign the TOP pointer is made to point the node after the first node, and the

other nodes remain unchanged.d. Free the allocated memory of the temporary pointer by using

RELEASENODE().

6. Display the result

7. Stop the program execution.

47

Page 48: DS Lab Manual[1]

PROGRAM:

RESULT:Thus the program for stack using linked list was executed successfully.

48

Page 49: DS Lab Manual[1]

Ex. No. 6 IMPLEMENTATION OF QUEUE USING ARRAYS

AIM:

To write a program to create, insert, delete and view nodes in the queue-using array.

CONCEPTS:

1. Start the program execution.

2. The basic operation performed area. Creation of Queueb. Enqueue operationc. Dequeue operationd. Display the queue

3. In creation of a Queue a. An array to hold the elements of the queue, which can be of any data types.b. The integer variable to indicate the top position of the stack within array.

4. In Enqueue operation a. Create a function enqueue() with two argument, the element to be added.b. Assign the new element in the array by incrementing the REAR variable.

5. In Dequeue operation a. Create a function Dequeue() with two argument, the address of the element to

store the dequeued value.b. Decrement the FRONT variable by 1.

6. Display the result

7. Stop the program execution.

49

Page 50: DS Lab Manual[1]

PROGRAM:

/* Queue using array */

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define size 5

struct queue

{

int que[size];

int front,rear;

}Q;

Qfull()

{

if(Q.rear >=size-1)

return 1;

else

return 0;

}

int insret(int item)

{

if(Q.front == -1)

Q.front++;

Q.que[++Q.rear] = item;

return Q.rear;

}

int Qempty()

{

if(Q.front == -1)|| (Q.front> Q.rear)

return 1;

else

return 0;

50

Page 51: DS Lab Manual[1]

}

int delete()

{

int item;

item= Q.que[Q.front];

Q.front++;

printf("\n The deleted item is %d,item);

return Q.front;

}

51

Page 52: DS Lab Manual[1]

RESULT:

Thus the program for Queue using array was executed successfully.

52

Page 53: DS Lab Manual[1]

Ex. No. 7 IMPLEMENTATION OF QUEUE USING LINKED LIST

AIM:

To write a program to create, insert, delete and view nodes in the queue-using linked list.

CONCEPTS:

1. Start the program execution.

2. The basic operation performed are Creation of Queue Enqueue operation Dequeue operation Display the queue

3. In creation of a stack An array to hold the elements of the queue, which can be of any data types. The integer variable to indicate the position of the queue.

4. In Enqueue operation Create a function Enqueue() with three argument. Create a new pointer to hold the new element. Allocate size of the stack node to the new pointer using GETNODE() The size of the stack depends on the heap memory is not available. Assign the value to the data of new pointer.

5. In Dequeue operation Create a function Dequeue() with three argument, the address of the element to

store the popped value. Create a temporary pointer to hold the removed element. Assign the FRONT pointer is made to point the node after the first node, and the

other nodes remain unchanged. Free the allocated memory of the temporary pointer by using RELEASENODE

().

6. Display the result

7. Stop the program execution.

53

Page 54: DS Lab Manual[1]

PROGRAM:

/* QUEUE using Linked List */

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define size 5

#include<math.h>

#include<string.h>

//#include<file.h>

typedef struct node

{

int data;

struct node *next;

}Q;

Q *front, *rear;

void main(void)

{

int choice;

void insert();

char ans;

clrscr();

Q.delete();

void display(Q *);

front=NULL;

rear=NULL;

do

{

printf("\n Main Menu");

printf("\n1.Insert\n2.Delete\n3.Display");

printf("\ Enter Your Choice");

scanf("%d",&choice);

54

Page 55: DS Lab Manual[1]

switch(choice)

{

case 1: insert();

break;

case 2:

front = delete();

//delete();

break;

case 3:

display(front);

// display();

break;

default: printf("\n u have entered the wrong choice\n");

break;

}

printf("\n Do u want to continue?\n");

flushall();

ans = getch();

}

while(ans =='y' || ans == 'Y');

getch();

clrscr();

}

Q *get_node(Q *temp)

{

temp= (Q*) malloc(sizeof(Q));

temp->next = NULL;

55

Page 56: DS Lab Manual[1]

return temp;

}

void insert()

{

char ch;

Q *temp;

clrscr();

temp = get_node(temp);

printf("\n\n Insert the element in the queue");

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

if(front == NULL)

{

front= temp;

rear=temp;

}

else

{

rear->next=temp;

rear=rear->next;

}

}

int Qempty(Q *front)

{

if(front ==NULL)

return 1;

else

return 0;

}

Q *delete()

//void delete()

56

Page 57: DS Lab Manual[1]

{

Q *temp;

temp=front;

if(Qempty(front))

{

printf("\n\n The Q is enpty");

printf("\n cannot be delete the element");

}

else

{

printf("\n \n The deleted elemnt is %d",temp->data);

front=front->next;

temp->next=NULL;

free(temp);

}

return front;

}

void display(Q *front)

//void display()

{

if(Qempty(front))

printf("\n \n The Q is empty \n");

else

{

printf("\n\n The dispaly of Q is \n");

for(;front !=rear->next;front=front->next)

printf(" \t %d",front->data);

}

getch();

}

57

Page 58: DS Lab Manual[1]

RESULT:

Thus the program for Queue using linked list was executed successfully.

58

Page 59: DS Lab Manual[1]

Ex.No.8 IMPLEMENTATION OF DIJIKSTRA’S SHORTEST PATH ALGORITHM

AIM:

Write a program to find the minimum distance from a node to all other nodes using Dijistra’s algorithm.

CONCEPTS:

1. Start the program execution.

2. In each iteration, we choose the minimum distance vertex from all unvisited vertices in the graph.

3. Visited nodes whose minimum distance from source node S is known.

4. Unvisited nodes where we don’t know the minimum distance from vertex S.

5. At each iteration we choose the unvisited node V of minimum distance from the source S, it is marked as visited node.

6. Since we know its distance from S update the distance to any unvisited node from V.

7. Display the result.

8. Stop the program execution.

59

Page 60: DS Lab Manual[1]

PROGRAM:

/* Djikstra's shortest path algo*/

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#define max 30

#define UNVISITED -1

#define VISITED 1

#define INFINITY 32767

typedef struct

{

int previous, len, status;

}node;

int searchpath(int src, int des, int pathmat[max], int *minLen);

void viewadjmat();

void viewpathmat(int pm[max],int n, int len);

int adjmat[max][max], n;

void main()

{

char ch, s, d;

int i,j,k,src,des,minlen,tot,pathmat[max];

FILE *f1;

printf("\n ENter the no of vertices of weighted graph:");

scanf("%d",&n);

if((f1 = fopen("D:\TURBOC\ASHOK\path.txt","rt")) == NULL)

{

fprintf(stderr,"cannot open ip file \n");

return;

}

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

60

Page 61: DS Lab Manual[1]

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

fscanf(f1,"%d",&adjmat[i][j]);

fclose(f1);

printf("\n The adj matrix is:\n\n");

viewadjmat();

while(1)

{

printf("\n ENter the Source node:");

fflush(stdin);

printf("\n Enter des node:");

fflush(stdin);

scanf("%c",&d);

src = toupper(s) - 64;

des = toupper(d) - 64;

tot = searchpath(src,des,pathmat,&minlen);

viewpathmat(pathmat,tot,minlen);

printf("\n Do u want to continue (y/n) : ");

ch = getche();

if(ch !='y' && ch != 'Y')

break;

}

}

void viewpathmat(int pm[max],int n, int len)

{

int k;

if(len !=0)

{

printf("\n minimum length is : %d \n\n Shortest path is :", len);

for(k=n;k>1;k--)

printf("%c -->",pm[k] + 64);

printf("%c\n",pm[k] + 64);

61

Page 62: DS Lab Manual[1]

printf("\n distance is: ");

for(k=n;k>1;k--)

printf("%d", adjmat[pm[k]][pm[k-1]]);

}

else

printf("\n NO path frm src to des node\n");

}

void viewadjmat()

{

int i,j;

printf("\n\n ");

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

printf("%4c", i+64);

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

printf("\n");

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

{

if(j == 1)

printf("%4c", i+64);

printf("%4d",adjmat[i][j]);

}}

int searchpath(int src,int des, int pathmat[max],int *minlen)

{

node graph[max];

int i, k, min,tot = 0, curvertex, newlen, u, v;

*minlen = 0;

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

{

graph[i].previous = 0;

graph[i].len = INFINITY;

graph[i].status = UNVISITED;

62

Page 63: DS Lab Manual[1]

}

graph[src].previous = 0;

graph[src].len = 0;

graph[src].status = VISITED;

curvertex = src;

while(curvertex !=des)

{

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

{

if(adjmat[curvertex][k] > 0 && graph[k].status == UNVISITED)

{

newlen = graph[curvertex].len + adjmat[curvertex][k];

if(newlen < graph[k].len)

{

graph[k].previous = curvertex;

graph[k].len = newlen;

}}}

min = INFINITY;

curvertex = 0;

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

if(graph[i].status == UNVISITED && graph[i].len < min)

{

min = graph[i].len;

curvertex = i;

}

if(curvertex == 0)

return 0;

graph[curvertex].status = VISITED;

}

while(curvertex !=0)

{

63

Page 64: DS Lab Manual[1]

pathmat[++tot] = curvertex;

curvertex = graph[curvertex].previous;

}

for(i=tot;i>1;i--)

{

u = pathmat[i];

v = pathmat[i-1];

*minlen = *minlen + adjmat[u][v];

}

return(tot);

}

64

Page 65: DS Lab Manual[1]

RESULT:

Thus the program to find the minimum distance from a node to all other nodes using Dijistra’s algorithm was executed successfully.

65

Page 66: DS Lab Manual[1]

Ex. No. 9 IMPLEMENTATION OF BINARY TREE TRAVERSALS

AIM:

To write a program to perform inorder, preorder and postorder traversal in binary search tree. CONCEPTS:

1. Start the program execution.

2. The basic operation performed are Creation of tree. Preorder traversal Postorder traversal Inorder traversal

3. In Preorder Traversal Process the root node Traverse the left subtree Traverse the right subtree.

4. In Postorder Traversal Traverse the left subtree Traverse the right subtree. Process the root node

5. In Inorder Traversal Traverse the left subtree Process the root node Traverse the right subtree.

6. Display the result

7. Stop the program execution.

66

Page 67: DS Lab Manual[1]

PROGRAM:

/* BINARY TREE TRAVERSAL */

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<alloc.h>

typedef struct node

{

int data;

struct node *left;

struct node *right;

}tree;

tree *getnode();

void readnode(tree *);

void releasenode(tree *head);

tree *createBTree();

tree *insertnode(tree *btree,tree *temp);

void preorder(tree *btree);

void postrder(tree *btree);

void inorder(tree *btree);

void levelorder(tree *btree);

tree *queue[30];

int front,rear;

int isEmpty();

void enqueue(tree *btree);

int dequeue(tree **dqdata);

void main()

{

int ch0oice;

tree *btree;

67

Page 68: DS Lab Manual[1]

printf("\n Create a new Binary tree \n");

btree = createBTree();

printf("\n TREE TRAVERSAL \n");

printf("\n \n 1.Preorder, \n 2.Inorder, \n 3. Postorder\n");

printf("\n ? ");

scanf("%d", &choice);

if(btree == NULL)

{

printf("BT is Empty");

return;

}

switch(choice)

{

case 1:

printf("\n Preorder");

preorder(btree);

break;

case 2:

printf("\n Inorder");

inorder(btree);

break;

case 3:

printf("\n Postorder");

postorder(btree);

break;

}

releasenode(btree);

}

tree *getnode()

{

int size;

68

Page 69: DS Lab Manual[1]

tree *newnode;

size = sizeof(tree);

newnode = (tree *)malloc(size);

return(newnode);

}

void readnode(tree *newnode)

{

printf("\n Enter the data");

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

newnode->left = NULL;

newnode->right = NULL;

}

void releasenode(tree *head){

free(head);

}

tree *createBTree()

{

char ch;

tree *btree = NULL, *temp;

do

{

temp = getnode();

readnode(temp);

btree = insertnode(btree,temp);

fflush(stdin);

printf("Do u wish to add data in the tree (y/n)? ");

scanf("%c",&ch);

}

while((ch == 'Y') || (ch == 'y'));

return btree;

}

69

Page 70: DS Lab Manual[1]

tree *insertnode(tree *btree, tree *temp)

{

if(btree == NULL)

return temp;

else if(temp->data < btree->data)

btree->left = insertnode(btree->left, temp);

else if(temp->data < btree->data)

btree->right = insertnode(btree->right, temp);

else if(temp->data == btree->data)

{

printf("\n Data is already Existing");

return btree;

}

return btree;

}

void inorder(tree *btree)

{

if(btree != NULL)

{

inorder(btree->left);

printf("%d", btree->data);

inorder(btree->right);

}

}

void preorder(tree *btree)

{

if(btree != NULL)

{

printf("%d", btree->data);

preorder(btree->left);

preorder(btree->right);

70

Page 71: DS Lab Manual[1]

}

}

void postorder(tree *btree)

{

if(btree != NULL)

{

postorder(btree->left);

postorder(btree->right);

printf("%d", btree->data);

}

}

int isempty()

{

if(front == -1 && rear == -1)

return 1;

else

return 0;

}

void enqueue(tree* btree)

{if(isempty())

front = rear = 0;

else

rear = rear +1;

queue[rear] = btree;

return 0;

}

int dequeue(tree **dqdata)

{

if(isempty())

return -1;

*dqdata = queue[front];

71

Page 72: DS Lab Manual[1]

if(front == rear)

front = rear = -1;

else

front = front + 1;

return 0;

}

72

Page 73: DS Lab Manual[1]

RESULT:

Thus the program for Binary Tree Traversal was executed successfully.

73

Page 74: DS Lab Manual[1]

Ex. No. 10 IMPLEMENTATION OF INSERTION SORT AND SELECTION SORT

AIM:

To Write a Program to sort the numbers using insertion sort and selection sort.

CONCEPTS:

1. Start the program execution.

2. In Insertion sort Get the numbers from keyboard, which is to be sorted. Each new inserted number is compared with numbers. The number is inserted in proper place by pushing one position to the left or

right. Repeat the step for the newly inserting elements until you get the sorted list.

3. In Selection sort Get the numbers from keyboard, which is to be sorted. Search smallest element in the list. When the element is found, it is swapped with the first element in the list. The second smallest element in the list is then searched. When the element is found, it is swapped with the second element in the list. Repeat the step until you get the sorted list.

4. Display the result.

5. Stop the program execution.

74

Page 75: DS Lab Manual[1]

PROGRAM:

RESULT:Thus the program for Insertion sort and selection sort was executed successfully.

75

Page 76: DS Lab Manual[1]

Ex. No. 11 IMPLEMENTATION OF SHELL SORT, QUICK SORT, AND MERGE SORT

AIM:

To Write a Program to sort the numbers using shell sort and quick sort, merge sort.

CONCEPTS:

1. Start the program execution.

2. In Shell sort Get the numbers from keyboard, which is to be sorted. Each new inserted number is compared only the consecutive elements and

interchanges the elements by only one space. Repeat the step for the newly inserting elements until you get the sorted list.

3. In Quick sort Get the numbers from keyboard, which is to be sorted. Divide the initial unsorted list into two parts, such that every element in the first

list is less than all the elements present in the second list. The procedure is repeated recursively for both the parts, up to relatively short

sequences, which can be sorted until the sequences, reduces to length ones.

4. In Merge sort Get the numbers from keyboard, which is to be sorted. To chop the list into two. If the list has even length, split the list into two equal sub lists. If the list has odd length, divide the list in two by making the first sub list one

entry greater then the second sub list. Then split both the sub lists in to two and go on until each of the sub lists are of

the size one. Finally, start merging the individual sub lists to obtain assorted list.

5. Display the result

6. Stop the program execution.

76

Page 77: DS Lab Manual[1]

PROGRAM:

RESULT:Thus the program for Shell sort and Quick sort, Merge sort was executed successfully.

77

Page 78: DS Lab Manual[1]

Ex. No. 12 IMPLEMENTATION OF RADIX SORT AND HEAP SORT

AIM:

To Write a Program to sort the numbers using insertion sort and selection sort.

CONCEPTS:

1. Start the program execution.

2. In Radix sort Get the numbers from keyboard, which is to be sorted. The values are successfully ordered on digit positions from right to left. This is accomplished by coping the values into bucket where the index is given

by the position of the digit being sorted. Once all the digit positions have been examined the list must be sorted.

3. In Heap sort Get the numbers from keyboard, which is to be sorted. The values are ordered on digit positions from left to right. Repeat the step until you get the sorted list.

4. Display the result

5. Stop the program execution.

78

Page 79: DS Lab Manual[1]

PROGRAM: RESULT:

Thus the program for Insertion sort and selection sort was executed successfully.

79