0% found this document useful (0 votes)
30 views12 pages

Data Structure AND Algorithm: Name: Mohammad Asim Jamal Reg. No.: 16BCE2240

The document contains code for implementing operations on a linked list and stack using linked lists. For the linked list, it includes code to insert elements at the beginning and a specified position, and delete elements from a specified position. For the stack, it implements push and pop operations to add and remove elements from the top of the stack, and functions to check if the stack is empty and return the top element. The output shows sample insertion and deletion of elements in the linked list and stack.

Uploaded by

Asim Jamal
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)
30 views12 pages

Data Structure AND Algorithm: Name: Mohammad Asim Jamal Reg. No.: 16BCE2240

The document contains code for implementing operations on a linked list and stack using linked lists. For the linked list, it includes code to insert elements at the beginning and a specified position, and delete elements from a specified position. For the stack, it implements push and pop operations to add and remove elements from the top of the stack, and functions to check if the stack is empty and return the top element. The output shows sample insertion and deletion of elements in the linked list and stack.

Uploaded by

Asim Jamal
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/ 12

DATA STRUCTURE

AND
ALGORITHM

Name: Mohammad Asim Jamal


Reg. No.: 16BCE2240

Exercise 3
1. Write programs to implement the following operations in a
linked list. Traversing a linked list (a)Inserting an element
at the beginning and (b) at any position (c) Deleting an
element from a specified position from a linked list

CODE:

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* head = NULL;

void insert_beg(int x)
{
struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head=temp;
}
void insert_pos(int x,int n)
{
struct Node* temp1 = (struct Node*) malloc(sizeof(struct Node));
temp1->data = x;
temp1->next = NULL;
if(n==1)
{
temp1->next=head;
head=temp1;
return;
}
struct Node* temp2=head;
for(int i=0;i<n-2;i++)
temp2=temp2->next;
temp1->next=temp2->next;
temp2->next=temp1;
}
void delete(int n)
{
struct Node* temp1=head;
if(temp1==NULL)
{
printf("List is Empty\n");
return;
}
if(n==1)
{
head=temp1->next;
free(temp1);
return;
}
for(int i=0;i<n-2;i++)
temp1=temp1->next;
struct Node* temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}
void Print()
{
struct Node* temp = head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}
void main()
{
head=NULL;
char ch,ch1='Y';
while(ch1=='Y')
{
printf("\n***Link List OPERATIONS:****\n");
printf("\n MENU \n");
printf("---------------------------------------\n");
printf("\n 1.Insert at Beginning \n");
printf("\n 2.Insert at nth Position \n");
printf("\n 3.Delete at nth Position \n");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int x;
printf("Enter a number which u want to enter ");
scanf("%d",&x);
insert_beg(x);
Print();
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
case 2:
{
int x,n;
printf("Enter a number which u want to enter ");
scanf("%d",&x);
printf("Enter a position where you want to enter ");
scanf("%d",&n);
insert_pos(x,n);
Print();
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
case 3:
{
int n;
printf("Enter a position which you want to delete ");
scanf("%d",&n);
delete(n);
Print();
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
}
}
}

OUTPUT:
Insert at Beginning: “246”
Insert at nth position: Enter 8 at 3rd position
Delete element from a list:
1. Design a program to show how a stack can be implemented by
using a linked list.
CODE:

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* top = NULL;

void Push(int x)
{
struct Node* NewNode = (struct Node*) malloc(sizeof(struct Node));
NewNode->data = x;
NewNode->link = top;
top=NewNode;
}

void Pop()
{
struct Node* temp = top;
if (top == NULL)
return;
top=top->link;
free(temp);
}
void Print()
{
struct Node* temp = top;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
printf("\n");

}
int IsEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}

int Top()
{
return (top->data);

}
void main()
{
char ch,ch1='Y';
while(ch1=='Y')
{
printf("\n***Stack Link OPERATIONS:****\n");
printf("\n MENU \n");
printf("---------------------------------------\n");
printf("\n 1.Create \n");
printf("\n 2.Delete \n");
printf("\n 3.Wish to see a Top Element \n");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int n;
printf("Enter a number which u want to enter ");
scanf("%d",&n);
Push(n);
Print();
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
case 2:
{
if(IsEmpty())
{
printf("Stack is Empty!!");
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
Pop();
Print();
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
case 3:
{
if(IsEmpty())
printf("Stack is Empty!!\n");
else
printf("%d\n",Top());
printf("Do you wish to continue ");
scanf(" %c",&ch1);
break;
}
}
}
}

OUTPUT is in next page


OUTPUT:

First will create stack of ‘975’


Now we print the top element of stack

Now we delete the element in the stack

You might also like