DS Lab 5-6 Programs
DS Lab 5-6 Programs
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *temp = NULL, *first = NULL, *second = NULL;
struct Node* Create(int student1 [], int n)
{
int i;
struct Node *t, *last;
temp = (struct Node *) malloc(sizeof(struct Node));
temp->data = student1 [0];
temp->next = NULL;
last = temp;
for (i = 1; i < n; i++)
{
t = (struct Node *) malloc(sizeof(struct Node));
t->data = student1 [i];
t->next = NULL;
last->next = t;
last = t;
}
return temp;
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf ("%d ", p->data);
p = p->next;
}
}
void Concat(struct Node *first, struct Node *second)
{
int i;
struct Node *p = first;
while (p->next != NULL)
{
p = p->next;
}
p->next = second;
second = NULL;
}
int searchElement(struct Node* head, int item)
{
struct Node* current = head; // Initialize current
int index = 0;
// traverse till then end of the linked list
while (current != NULL)
{
if (current->data == item){
printf("item present at index %d",index);
return index;
}
current = current->next;
index++;
}
printf("element not found");
return -1;
}
int main()
{
int student1[] = { 9, 7, 4, 3 };
int student2[] = { 2, 5, 6, 8 };
int key;
struct node *p;
first = Create(student1, 4);
second = Create(student2, 4);
printf ("1st Student List: ");
Display (first);
printf ("\n2nd Student List: ");
Display (second);
Concat (first, second);
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe Professor node with the ID:%s is deleted", first->ID);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
printf("\nThe Professor node with the ID:%s is deleted",temp->ID);
free(temp);
count--;
return first;
}
void display()
{
NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||ID:%s|Name:%s|Branch:%s|specialization:%s",
nodeno, cur->ID, cur->name,cur->branch, cur->specialization);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of Professor nodes is %d",count);
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Professor Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtFront");
printf("\n4:DeleteAtFront");
printf("\n5:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of Professor : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertfront();
break;
case 2: display(); break;
}
}
}
****************************************************************
6.b) Create a DLL queue of N professor’s Data
*************************************************************
#include<stdio.h>
#include<stdlib.h>
struct node
{
char ID[25],name[25],branch[10],specialization[25];
struct node *llink;
struct node *rlink;
};
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ID,Name,Branch,specialization \n");
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
NODE deleteEnd()
{
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink == NULL)
{
printf("\nThe professor node with the ID:%s is deleted",first->ID);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;
}
cur->llink = NULL;
printf("\nThe professor node with the ID:%s is deleted",cur->ID);
free(cur);
prev->rlink = NULL;
count--;
return first;
}
void display()
{
NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||ID:%s|Name:%s|Branch:%s|specialization:%s",
nodeno, cur->ID, cur->name,cur->branch, cur->specialization);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of Professor nodes is %d",count);
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Professor Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtFront");
printf("\n4:DeleteAtEnd");
printf("\n5:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of Professor : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertfront();
break;