You are on page 1of 24

G.

SHIYAM SUNDARAM
20BIT0100

ITE 1004 – DATA STRUCTURES AND ALGORITHMS

ASSESMENT - 04

IMPLEMENTATION OF DOUBLY LINKED LIST :

C code:

/*IMPLEMENTATION OF DOUBLY LINKED LIST*/

#include<stdio.h>
#include<stdlib.h>
int count=0;
struct node{
int data;
struct node *prev;
struct node *next;
};
struct node *newnode,*temp,*tail,*index,*current;
struct node *head=NULL;
void create()
{
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data :\n");
scanf("%d",&newnode->data);
count=count+1;
if(head==NULL)
{
tail=head=temp=newnode;
}
else
{
tail->next=newnode;
newnode->prev=tail;
G. SHIYAM SUNDARAM
20BIT0100

tail=tail->next;
}
}
}
void insertbeg()//insert at the beginning this is useful for stack operation
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data :\n");
scanf("%d",&newnode->data);
newnode->next=head;
head->prev=newnode;
head=newnode;
}
void insertend()//insert at the end this is useful for queue operation
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data :\n");
scanf("%d",&newnode->data);
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
void insertatpos()//insert after a specific position in the linked list
{
int pos;
printf("please enter the position:\n");
scanf("%d",&pos);
if(pos<1)
{
printf("invalid position\n");
}
else if(pos==1)
{
G. SHIYAM SUNDARAM
20BIT0100

insertbeg();
}
else if(pos==count)
{
insertend();
}
else
{
temp=head;
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data :");
scanf("%d",&newnode->data);
int i=1;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
}
}
void deletebeg()//delete from beginning
{
if(head==0)
{
printf("the list is empty\n");
}
else
{
temp=head;
G. SHIYAM SUNDARAM
20BIT0100

head=head->next;
head->prev=0;
free(temp);
}
}
void deleteend()//delete from the end of the list
{
if(tail==0)
{
printf("the list is empty\n");
}
else
{
temp=tail;
tail->prev->next=0;
tail=tail->prev;
free(temp);
}
}
void deleteatpos()//delete at a specific position
{
printf("enter the position : \n");
int pos;
scanf("%d",&pos);
if(pos==1)
{
deletebeg();
}
else if(pos==count)
{
deleteend();
}
else if(pos<1||pos>count)
G. SHIYAM SUNDARAM
20BIT0100

{
printf("please enter a valid position\n");
}
else
{
temp=head;
int i=1;
for(i=1;i<pos;i++)
{
temp=temp->next;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
}
void ascendingorder()//to arrange the data in ascending order
{
int a;
if(head==NULL)
{
printf("the list is empty");
}
else
{
current=head;
while(current->next!=0)
{
index=current->next;
while(index!=0)
{
if(current->data>index->data)
{
G. SHIYAM SUNDARAM
20BIT0100

a=current->data;
current->data=index->data;
index->data=a;
}
index=index->next;
}
current=current->next;
}
}
}
void descendingorder()//to arrange the data in descending order
{
int a;
if(head==NULL)
{
printf("the list is empty");
}
else
{
current=head;
while(current->next!=0)
{
index=current->next;
while(index!=0)
{
if(current->data<index->data)
{
a=current->data;
current->data=index->data;
index->data=a;
}
index=index->next;
}
G. SHIYAM SUNDARAM
20BIT0100

current=current->next;
}
}

}
void display()
{
{
temp=head;
while(temp!=0)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}
}
int main()
{
int a;
while(1)
{
printf("please enter the option :\n");
printf("1.create \n 2.display\n 3.insert at beg\n 4. insert end \n 5. insert
at pos\n");
printf("6.delete at beg \n 7. delete at end \n 8.delete at position \n 9.
exit\n 10.ascendingorder \n 11.descendingorder\n ");
scanf("%d",&a);
switch(a)
{
case(1):
create();
break;
G. SHIYAM SUNDARAM
20BIT0100

case (2):
display();
break;
case (3):
insertbeg();
break;
case (4):
insertend();
break;
case (5):
insertatpos();
break;
case (6):
deletebeg();
break;
case (7):
deleteend();
break;
case (8):
deleteatpos();
break;
case (9):
exit(0);
case (10):
ascendingorder();
break;
case (11):
descendingorder();
break;
default:
printf("please enter a valid position \n");
}
}
G. SHIYAM SUNDARAM
20BIT0100

return 0;
}

COMMAND WINDOW:
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100

IMPLEMENTATION OF QUEUE USING LINKED LIST ( HERE WE HAVE USED


DOUBLY LINKED LIST):

C-code:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *prev;
struct node *next;
};
int count=0;
struct node *newnode,*temp,*tail,*index,*current;
struct node *head=NULL;
void create()
{
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data :\n");
scanf("%d",&newnode->data);
count=count+1;
if(head==NULL)
{
tail=head=temp=newnode;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=tail->next;
}
}
}
void enqueue()
{
G. SHIYAM SUNDARAM
20BIT0100

newnode=(struct node *)malloc(sizeof(struct node));


printf("enter the data :\n");
scanf("%d",&newnode->data);
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
void dequeue()
{
if(head==0)
{
printf("the list is empty\n");
}
else
{
temp=head;
head=head->next;
head->prev=0;
printf("%d\n",temp->data);
free(temp);
}
}
void display()
{
{
temp=head;
while(temp!=0)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}
}
int main()
{
G. SHIYAM SUNDARAM
20BIT0100

int a;
while(1)
{
printf("please enter the option :\n");
printf("1.create \n 2.display\n 3.enqueue\n 4. dequeue \n ");
scanf("%d",&a);
switch(a)
{
case(1):
create();
break;
case (2):
display();
break;
case (3):
enqueue();
break;
case (4):
dequeue();
break;
case (5):
exit(0);
}
}
return 0;
}
G. SHIYAM SUNDARAM
20BIT0100

COMMAND WINDOW:
G. SHIYAM SUNDARAM
20BIT0100
G. SHIYAM SUNDARAM
20BIT0100

3. IMPLEMENTATION OF STACK USING LINKED LIST:

C CODE:

#include <stdio.h>
#include<malloc.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *newnode,*temp,*head=NULL;
int data;
int i,n;
void createlist(int n);//DECLARATION OF THE FUNCTIONS//
void traverse();
void insertbeg();
void deletebeg();
int main()
{
int k,l,m;
printf("enter the number of entries:");
scanf("%d",&n);
createlist(n);
printf("\ndata in the list\n");
traverse();
while(1)
{
printf("1. push \n 2. pop \n 3.display \n 4. exit");
scanf("%d",&k);
switch(k)
{
case(1):
insertbeg();
break;
case(2):
deletebeg();
G. SHIYAM SUNDARAM
20BIT0100

break;
case(3):
traverse();
break;
case(4):
exit(0);
}
}
return 0;
}
void createlist(int n)//initialize a stack
{
for(i=0;i<n;i++)
{
newnode=(struct node *)malloc(sizeof(struct node));//MEMORY
ALLOCATION TO NEW NODE//
if(newnode==NULL)
{
printf("unable to allocate memory");
exit(0);
}
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=temp=newnode;//HEAD POINTS TO THE FIRST NODE//
}
else
{
temp->next=newnode;
temp=newnode;
}
}
}
void traverse()//display
G. SHIYAM SUNDARAM
20BIT0100

{
temp=head;
while(temp!=0)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
void insertbeg()//push operation
{
if(head==0)
{
newnode=(struct node *)malloc(sizeof(struct node));//MEMORY
ALLOCATION TO NEW NODE//
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=0;
}
else
{
newnode=(struct node *)malloc(sizeof(struct node));//MEMORY
ALLOCATION TO NEW NODE//
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
}
}
void deletebeg()//pop operation
{
temp=head;
head=temp->next;
printf("%d\n",temp->data);
free(temp);//MEMORY MUST BE FREED TO AVOID GARBAGE VALUES//
}
G. SHIYAM SUNDARAM
20BIT0100

COMMAND WINDOW:
G. SHIYAM SUNDARAM
20BIT0100

You might also like