Data Structure Assignment
Data Structure Assignment
#include<stdlib.h>
struct node
int data;
};
while(ptr!=NULL)
printf("\n%d",ptr->data);
ptr=ptr->next;
newnode->next=head;
head=newnode;
Linked_list(head);
struct node*ptr=NULL;
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
ptr->next=newnode;
newnode->next=NULL;
Linked_list(head);
int p,i;
scanf("%d",&p);
struct node*ptr=NULL;
ptr=head;
if(n<p)
printf("invalid input");
else
for(i=1;i<p-1;i++)
ptr=ptr->next;
newnode->next=ptr->next;
ptr->next=newnode;
Linked_list(head);
int main()
struct node*head,*ptr,*newnode;
head=0;
int n,i,k;
scanf("%d",&n);
for(i=1;i<=n;i++)
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==0)
head=ptr=newnode;
else
ptr->next=newnode;
ptr=newnode;
Linked_list(head);
printf("\n");
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
scanf("%d",&k);
if(k==1)
insertB(head,newnode);
else if(k==2)
insertE(head,newnode);
else if(k==3)
insertP(head,newnode,n);
AT BEGINNING:
AT END:
AT GIVEN POSITION :
• Delete a Node at Beginning, at Ending and at a given
Position
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node*next;
};
while(ptr!=0)
printf("\n%d",ptr->data);
ptr=ptr->next;
{
struct node*ptr=NULL;
ptr=head;
head=head->next;
free(ptr);
printf("\n");
Linked_list(head);
struct node*ptr=head;
struct node*q=head->next;
while(q->next!=NULL)
ptr=ptr->next;
q=q->next;
ptr->next=NULL;
free(q);
printf("\n");
Linked_list(head);
int p,i;
struct node*ptr=head;
scanf("%d",&p);
if(n<p)
printf("invalid");
else
{
for(i=1;i<p-1;i++)
ptr=ptr->next;
struct node*q=ptr->next;
ptr->next=q->next;
free(q);
printf("\n");
Linked_list(head);
int main()
struct node*head,*ptr,*newnode;
head=0;
int n,i,k;
scanf("%d",&n);
for(i=0;i<n;i++)
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==0)
head=ptr=newnode;
else
ptr->next=newnode;
ptr=newnode;
}
Linked_list(head);
printf("\n");
scanf("%d",&k);
if(k==1)
deleteB(head);
else if(k==2)
deleteE(head);
else if(k==3)
deleteP(head,n);
else
printf("invalid number");
AT BEGINNING:
AT END:
AT GIVEN NODE:
• Search, Count the Number of Nodes and Display
#include <stdio.h>
struct node{
int data;
};
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
tail = newNode;
}
}
int countNodes() {
int count = 0;
while(current != NULL) {
count++;
current = current->next;
return count;
void display() {
if(head == NULL) {
printf("List is empty\n");
return;
while(current != NULL) {
current = current->next;
}
printf("\n");
int main()
addNode(1);
addNode(2);
addNode(3);
addNode(4);
display();
return 0;
OUTPUT:
struct node
{
int data;
struct node*next;
struct node*prev;
};
int main()
{
struct node*head,*ptr,*newnode,*newnode2;
int n,i,k;
head=0;
printf("enter the number nodes to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
newnode->prev=ptr;
ptr=newnode;
}
}
Dlinked_list(head,newnode);
printf("\nenter the number to be inserted");
newnode2=malloc(sizeof(struct node));
scanf("%d",&newnode2->data);
printf("enter number for insertion 1-begining,2-ending,3-position");
scanf("%d",&k);
if(k==1)
insertionB(head,newnode2);
if(k==2)
insertionE(head,newnode2);
if(k==3)
insertionP(head,newnode2,n);
else
printf(" ");
}
OUTPUT:
AT BEGINNING:
AT END:
AT GIVEN NODE:
OUTPUT:
AT BEGINNING:
AT END:
AT GIVEN NODE:
• Search, Count the Number of Nodes and Display
#include<stdio.h>
#include<stdlib.h>
void create();
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL,*temp,*newnode;
void create()
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
if(head==NULL)
{
newnode->next=0;
newnode->prev=0;
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
}
void display()
{
int r=0;
temp=head;
while(temp!=0)
{
r++;
printf("%d\n",temp->data);
temp=temp->next;
}
printf("Total no. of nodes:%d\n",r);
}
int main()
{
int n,h,c,k=0;
printf("Enter no. of nodes :\n");
scanf("%d",&n);
printf("Enter the elements of node:\n");
for(int i=0;i<n;i++)
create();
printf("The double linked list :\n");
display();
printf("The element to be searched:\n");
scanf("%d",&c);
temp=head;
while(temp!=0)
{
k+=1;
if (temp->data==c)
{
printf("Element found at position:%d",k);
break;
}
temp=temp->next;
}
if(temp==0)
printf("Element is not found");
}
OUTPUT:
Create a Circular singly linked list for adding and
deleting a Node.
ADDING:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node*next;
int data;
};
void Clinked_list(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}
OUTPUT:
DELETING:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node*next;
int data;
};
void Clinked_list(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}
void CdeleteB(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
struct node*p=head;
head=head->next;
ptr->next=head;
free(p);
Clinked_list(head);
}
void CdeleteE(struct node*head)
{
struct node*p=head;
struct node*q=head->next;
while(q->next!=head)
{
q=q->next;
p=p->next;
}
p->next=head;
free(q);
Clinked_list(head);
}
int main()
{
struct node*head,*ptr,*newnode;
head=0;
int n,i,k;
printf("enter the value to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
ptr=newnode;
}
ptr->next=head;
}
Clinked_list(head);
printf("\n");
printf("enter the element to be deleted at 1-beginning,2-ending");
scanf("%d",&k);
if(k==1)
CdeleteB(head);
if(k==2)
CdeleteE(head);
else
{
printf("invalid");
}
}
OUTPUT: