0% found this document useful (0 votes)
63 views8 pages

DS Lab 5-6 Programs

The document discusses creating and implementing operations on doubly linked lists (DLLs) of professor data in C. It defines a node structure to store professor ID, name, branch, and specialization. Functions are created to insert nodes at the front of the list, delete nodes from the front, and display the list. The main function implements a menu-driven program to: 1) Create a DLL stack by inserting multiple nodes at the front, 2) Display the list, 3) Insert a new front node, 4) Delete the front node.

Uploaded by

mumthaz
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)
63 views8 pages

DS Lab 5-6 Programs

The document discusses creating and implementing operations on doubly linked lists (DLLs) of professor data in C. It defines a node structure to store professor ID, name, branch, and specialization. Functions are created to insert nodes at the front of the list, delete nodes from the front, and display the list. The main function implements a menu-driven program to: 1) Create a DLL stack by inserting multiple nodes at the front, 2) Display the list, 3) Insert a new front node, 4) Delete the front node.

Uploaded by

mumthaz
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/ 8

5. b) LINEAR SEARCH.

Create a ssl queue of N students data concatenation of two SLL of


integers

#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);

printf ("\n\nConcantenated student List: \n");


Display (first);
printf ("\nenter the ele to be searched ");
scanf("%d",&key);
searchElement(first,key);
return 0;
}
6. Design, develop implement menu driven program in c for the following operations of DLL of
professor data with the fields:ID,Name, Branch, Area of specialization
a. Create a DLL stack 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;
};

typedef struct node* NODE;


NODE first = NULL;
int count=0;

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");


scanf("%s %s %s %s", enode->ID, enode->name, enode->branch, enode->specialization);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}

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;

case 3: first = insertfront(); break;

case 4: first = deletefront(); break;


case 5: exit(0); break;

default: printf("\nPlease Enter the valid choice");

}
}
}
****************************************************************
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;
};

typedef struct node* NODE;


NODE first = NULL;
int count=0;

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");

scanf("%s %s %s %s", enode->ID, enode->name, enode->branch, enode->specialization);


enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}

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;

case 2: display(); break;

case 3: first = insertfront(); break;

case 4: first = deleteEnd(); break;


case 5: exit(0); break;
default: printf("\nPlease Enter the valid choice");
}
}
}

You might also like