Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Unit-5

1. Implement a C program to create and concatenate two singly linked lists. CO1,BL1
#include<stdio.h>
#include<stdlib.h>
 
struct node
{
        int info;
        struct node *link;
};
struct node *create_list(struct node *);
struct node *concat( struct node *start1,struct node *start2);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start,int data);
void display(struct node *start);
 
int main()
{
        struct node *start1=NULL,*start2=NULL;
        start1=create_list(start1);
        start2=create_list(start2);
        printf("\nFirst list is  : ");
        display(start1);
        printf("\nSecond list is  : ");
        display(start2);
    start1=concat(start1, start2);
        printf("\nConcatenated list is  : ");
        display(start1);
 
        return 0;
 
}/*End of main()*/
 
struct node *concat( struct node *start1,struct node *start2)
{
        struct node *ptr;
        if(start1==NULL)
        {
                start1=start2;
                return start1;
        }
        if(start2==NULL)
                return start1;
        ptr=start1;
        while(ptr->link!=NULL)
                ptr=ptr->link;
        ptr->link=start2;
        return start1;
}
struct node *create_list(struct node *start)
{
        int i,n,data;
        printf("\nEnter the number of nodes : ");
        scanf("%d",&n);
        start=NULL;
        if(n==0)
                return start;
 
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        start=addatbeg(start,data);
 
        for(i=2;i<=n;i++)
        {
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                start=addatend(start,data);
        }
        return start;
}/*End of create_list()*/
 
void display(struct node *start)
{
        struct node *p;
        if(start==NULL)
        {
                printf("\nList is empty\n");
                return;
        }
        p=start;
        while(p!=NULL)
        {
                printf("%d ", p->info);
                p=p->link;
        }
        printf("\n");
}/*End of display() */
 
struct node *addatbeg(struct node *start,int data)
{
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        tmp->link=start;
        start=tmp;
        return start;
}/*End of addatbeg()*/
 
struct node *addatend(struct node *start, int data)
{
        struct node *p,*tmp;
        tmp= (struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        p=start;
        while(p->link!=NULL)
                p=p->link;
        p->link=tmp;
        tmp->link=NULL;
        return start;
}/*End of addatend()*/

2. What is linked list? Write advantages & disadvantages singly linked list. . CO2,BL1

Singly Linked List is a linear and dynamic data structure. It stores elements at non-contiguous
memory locations.

linked list representation

It consists of a group of nodes in a sequence and each node has two fields, one contains data and
the other contains the address of the next node.

The linked list does not allow direct access to the element. To access any particular element you
have to start at the head and traverses each node until you get to that particular item.

Advantages:-
1) Insertions and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,even then we can store
the data in computer.
7) It is less expensive.

Disadvantages:-

1) It requires more space as pointers are also stored with information.


2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those elements that
come before that element.
4) we cannot traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.

3. What is a stack? Write a program for linked list representation of stack. . CO3,BL3

Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Stack Illustration
There are many real-life examples of a stack. Consider an example of plates stacked over one
another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.

Mainly the following three basic operations are performed in the stack:
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.

Pop: Removes an item from the stack. The items are popped in the reversed order in which
they are pushed. If the stack is empty, then it is said to be an Underflow condition.
Peek or Top: Returns the top element of the stack.

isEmpty: Returns true if the stack is empty, else false.

Implementation: 

There are two ways to implement a stack: 

Using array

Using linked list

// C program for linked list implementation of stack


#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a stack


struct StackNode {
int data;
struct StackNode* next;
};

struct StackNode* newNode(int data)


{
struct StackNode* stackNode =
(struct StackNode*)
malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty(struct StackNode* root)


{
return !root;
}

void push(struct StackNode** root, int data)


{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
printf("%d pushed to stack\n", data);
}
int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);

return popped;
}

int peek(struct StackNode* root)


{
if (isEmpty(root))
return INT_MIN;
return root->data;
}

int main()
{
struct StackNode* root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);

printf("%d popped from stack\n", pop(&root));

printf("Top element is %d\n", peek(root));

return 0;
}
4.Discuss about linked list implementation of Queue ADT. . CO4, BL4

Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO).

 A good example of queue is any queue of consumers for a resource where the consumer
that came first is served first. 
The difference between stacks and queues is in removing. In a stack we remove the item
the most recently added; in a queue, we remove the item the least recently added.

Operations on Queue: 

Mainly the following four basic operations are performed on queue:


Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition. 

Dequeue: Removes an item from the queue. The items are popped in the same order in
which they are pushed. If the queue is empty, then it is said to be an Underflow
condition. 

Front: Get the front item from queue.


 
Rear: Get the last item from queue. 
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct queue {
int sno;
char sname[20];
int tmarks;
struct queue* next;
};
struct queue* front=NULL;
struct queue* rear=NULL;
void enqueue(int no,char name[],int marks){
struct queue* temp=(struct queue*)malloc(sizeof(struct queue));
temp->sno=no;
strcpy(temp->sname,name);
temp->tmarks=marks;
temp->next=NULL;
if(front==NULL){
front=rear=temp;
}
else{
rear->next=temp;
rear=temp;
}
}
void dequeue(){
struct queue* dtemp=front;
if(front==NULL){
printf("queue is empty\n");
}
else{
front=front->next;
free(dtemp);
}
}
void display(){
struct queue* disp=front;
if(front==NULL){
printf("queue is empty\n");
}
else{

while(disp!=NULL){
printf("%d %s %d\n",disp->sno,disp->sname,disp->tmarks);
disp=disp->next;
}
}
}
int main(){
int i,n,id,marks;
char sname[10];
printf("Enter no.of students\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter id name marks\n");
scanf("%d %s %d",&id,sname,&marks);
enqueue(id,sname,marks);
}
printf("data in queue\n");
display();
dequeue();
printf("after removing the front node\n");
display();
}

5. Explain array representation of a stack. . CO1,BL1


A stack is a data structure that can be represented as an array. Let us learn more about Array
representation of a stack –
An array is used to store an ordered list of elements. Using an array for representation of stack is
one of the easy techniques to manage the data. But there is a major difference between an array
and a stack.
● Size of an array is fixed.
● While, in a stack, there is no fixed size since the size of stack changed with the number of
elements inserted or deleted to and from it.
Despite the difference, an array can be used to represent a stack by taking an array of maximum
size; big enough to manage a stack.
For Example:
We are given a stack of elements: 12 , 08 , 21 , 33 , 18 , 40.

// C program for array implementation of stack

#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

// A structure to represent a stack

struct Stack {

int top;

unsigned capacity;

int* array;

};

// function to create a stack of given capacity. It initializes size of

// stack as 0
struct Stack* createStack(unsigned capacity)

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (int*)malloc(stack->capacity * sizeof(int));

return stack;

// Stack is full when top is equal to the last index

int isFull(struct Stack* stack)

return stack->top == stack->capacity - 1;

// Stack is empty when top is equal to -1

int isEmpty(struct Stack* stack)

return stack->top == -1;

// Function to add an item to stack. It increases top by 1

void push(struct Stack* stack, int item)


{

if (isFull(stack))

return;

stack->array[++stack->top] = item;

printf("%d pushed to stack\n", item);

// Function to remove an item from stack. It decreases top by 1

int pop(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top--];

// Function to return the top from stack without removing it

int peek(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top];

}
// Driver program to test above functions

int main()

struct Stack* stack = createStack(100);

push(stack, 10);

push(stack, 20);

push(stack, 30);

printf("%d popped from stack\n", pop(stack));

return 0;

6. Write an algorithm to insert a new node at the beginning, at middle position and at the
end of a single linked list. . CO1,BL1
Ans:
Algorithm
1. Create a class Node which has two attributes: data and next. Next is a pointer to the next
node in the list.
2. Create another class InsertMid which has three attributes: head, tail, and size that keep
tracks of a number of nodes present in the list.
3. addNode() will add a new node to the list:

0.
Create a new node.
a.
It first checks, whether the head is equal to null which means the list is empty.
b.
If the list is empty, both head and tail will point to a newly added node.
c.
If the list is not empty, the new node will be added to end of the list such that tail's
next will point to a newly added node. This new node will become the new tail of
the list.
addInMid() will add a new node at the middle of the list:
0. It first checks, whether the head is equal to null which means the list is empty.
a. If the list is empty, both head and tail will point to a newly added node.
b. If the list is not empty, then calculate the size of the list and divide it by 2 to get
mid-point of the list.
c. Define node current that will iterate through the list till current will point to mid
node.
d. Define another node temp which will point to node next to current.
e. The new node will be inserted after current and before temp such that current will
point to the new node and the new node will point to temp.

7. Write a program to create a linked list of ‘n’ integers and perform following operations
using functions
i. Sum of elements
ii. Maximum
iii. Minimum
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
int find_sum(struct Node* t)
{
if(t==NULL)
{
return 0;
}
else
{
return t->data+find_sum(t->next);
}
}
int find_max(struct Node*t)
{
if(t==NULL)
{
return -1;

}
else if(t->next==NULL)
{
return t->data;
}
else
{
int m=find_max(t->next);
if(t->data>m)
{
return t->data;
}
else
{
return m;
}

int find_min(struct Node*t)


{
if(t==NULL)
{
return -1;
}
else if(t->next==NULL)
{
return t->data;
}
else
{
int m=find_min(t->next);
if(t->data<m)
{
return t->data;
}
else
{
return m;
}

struct Node *head=NULL;


struct Node *addNode(int d){
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->data=d;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
return head;
}
void display(struct Node*t)
{
if(t==NULL)
{
return ;
}
else
{
printf("%d\n",t->data);
display(t->next);
}
}
int main()
{
struct Node *t;
int sum=0;
addNode(25);
addNode(40);
addNode(34);
addNode(11);
addNode(22);
for(t=head;t!=NULL;t=t->next)
{
printf("%d\n",t->data);
}
for(t=head;t!=NULL;t=t->next)
{
sum=sum+t->data;
}
printf("\nSum is =%d\n",sum);
printf("\n recursive sum =%d \n",find_sum(head));
printf("\n maxi in list =%d\n",find_max(head));
printf("\n mini in list =%d\n",find_min(head));

display(head);
}

9. Write a program to create a linked list and perform following operations using functions
i. delete by value ii. delete by position
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *head=NULL;
struct Node *delNode1(int pos)
{
struct Node *temp;
if(pos==0)
{
temp=head;
head=head->next;
free(temp);
return head;
}
else
{
int i=0;
struct Node *dptr;
temp=head;
while(i<(pos-1))
{
temp=temp->next;
i=i+1;
}
dptr=temp->next;
temp->next=temp->next->next;
free(dptr);
return head;
}
}
struct Node *delNode(int val)
{
struct Node *temp,*t;
for(t=head;t!=NULL;t=t->next)
{

if(t->data==val)
{
temp=head;
head=head->next;
free(temp);
}

}
return head;
}
int find_min(struct Node*t)
{
if(t==NULL)
{
return -1;

}
else if(t->next==NULL)
{
return t->data;
}
else
{
int m=find_min(t->next);
if(t->data<m)
{
return t->data;
}
else
{
return m;
}

}
}

struct Node *addNode(int d){


struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->data=d;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
return head;
}

int main()
{
struct Node *t;
int sum=0;
addNode(25);
addNode(40);
addNode(34);
addNode(11);
addNode(22);
for(t=head;t!=NULL;t=t->next)
{
printf("%d\n",t->data);
}
head=delNode1(0);
printf("\nList After deletion\n");
for(t=head;t!=NULL;t=t->next)
{
printf("%d\n",t->data);
}
head=delNode(11);
printf("\nList After deletion\n");
for(t=head;t!=NULL;t=t->next)
{
printf("%d\n",t->data);
}
// printf("\n mini in list =%d\n",find_min(head));
}
10. Write a program to create a linked list and perform the following operations:
i. Search an element in the linked list
ii. Reverse the linked list and display
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *head=NULL;

struct Node *addNode(int d){


struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->data=d;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
temp->next=head;

head=temp;

}
return head;
}
int search(int val)
{
struct Node *t=head;
for(t=head;t!=NULL;t=t->next)
{
if(t->data==val)
{
return 1;
}
}
return -1;
}
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;

// Reverse current node's pointer


current->next = prev;

// Move pointers one position ahead.


prev = current;
current = next;
}
*head_ref = prev;
}
void display(struct Node*t)
{
if(t==NULL)
{
return ;
}
else
{
printf("%d\n",t->data);
display(t->next);
}
}
int main()
{
struct Node *t;
int sum=0;
addNode(25);
addNode(40);
addNode(34);
addNode(11);
addNode(22);
for(t=head;t!=NULL;t=t->next)
{
printf("%d\n",t->data);
}
if(search(41)==1){
printf("element found \n");
}
else{
printf("element not found");
}

reverse(&head);
display(head);
}
11. Explain and implement Queue using array representation.
#include<stdio.h>
#include<conio.h>
#define SIZE 10

void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;

void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}

12. Create a linked list with student details (ID, name, CGPA) and find the student with highest
CGPA.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
int ID;
char name[20];
int CGPA;
struct Node *next;
};
struct Node *head=NULL;
struct Node *addNode(int d,char n[],int c)
{
struct Node *temp;
temp=(struct Node*)malloc(sizeof(struct Node));
temp->ID=d;
strcpy(temp->name,n);
temp->CGPA=c;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
return head;
}
int find_max(struct Node*t)
{
if(t==NULL)
{
return -1;

}
else if(t->next==NULL)
{
return t->CGPA;
}
else
{
int m=find_max(t->next);
if(t->CGPA>m)
{
return t->CGPA;
}
else
{
return m;
}

}
}
int main()
{
int n,o,p,i;
char q[100];
struct Node *t;
printf("enter the no of employee\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the id of employee:\n");
scanf("%d",&o);
printf("enter the name of employee:\n");
scanf("%s",q);
printf("enter the cgpa of employee:\n");
scanf("%d",&p);
addNode(o,q,p);
}
for(t=head;t!=NULL;t=t->next)
{
printf("The id of employe is:%d\n",t->ID);
printf("the name of employe is :%s\n",t->name);
printf("the cgpa is :%d\n",t->CGPA);
}
int t1=find_max(head);
printf("the max cgpa is :%d\n",t1);
}

You might also like