0% found this document useful (0 votes)
14 views9 pages

Assignment 7

Uploaded by

Naman
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)
14 views9 pages

Assignment 7

Uploaded by

Naman
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/ 9

Data Structures in C

ASSIGNMENT-7

Name: Naman Kumar

Course: BTECH(CSE)

Section: B1

Class Roll No.: 45

University Roll No.: 2319143


Question: Implement linked list
Algorithm
Step.1: Define a self-referential structure node with:
1. Information
2. Pointer referring to another structure
Step.2: Initialize a node type pointer head with NULL
Step.3: In function insert if head=NULL then insert the
Value in the info part of head and it’s pointer to
NULL
else, initialize a local node pointer temp and
store value in it and head=temp
return head
Step.3: In deletion func. If pointer head is pointing to
NULL then print “linked list is empty”
else, move head to one step
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node node;
node *insert(node *head,int v)
{
if(head==NULL)
{
head=(node *)malloc(sizeof(node));
head->info=v;
head->next=NULL;
return head;
}
else
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->info=v;
temp->next=head;
head=temp;
}
return head;
}
node* pop(node *head)
{
if(head==NULL)
{
printf("Empty");
return head;
}
node *temp;
temp=head;
head=head->next;
printf("Deleted element is %d",temp->info);
return head;

}
void display(node *head)
{
while(head!=NULL)
{
printf(" %d",head->info);
head=head->next;
}
}
int main()
{
node *head = NULL;
head=insert(head,10);
head=insert(head,20);
head=insert(head,30);
display(head);
pop(head);
display(head);
return 0;
}
Question: Adding node at the end of the
linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node node;
void inserttail(node**head,node**last,int v)
{
node*temp=(node*)malloc(sizeof(node));
temp->info=v;
temp->next=NULL;
if((*head)==NULL)
{
(*head)=temp;
(*last)=temp;
}
else
{
(*last)->next=temp;
(*last)=temp;
}
}
void display(node *head)
{
printf("\n");
while(head!=NULL)
{
printf(" %d",head->info);
head=head->next;
}
}
node* deletion(node *head)
{
if(head==NULL)
{
printf("Empty");
return head;
}
node *temp;
temp=head;
head=head->next;
printf("\nDeleted element is %d",temp->info);
return head;
}
int main()
{
node *head = NULL,*last=NULL;
inserttail(&head,&last,10);
inserttail(&head,&last,20);
inserttail(&head,&last,30);
inserttail(&head,&last,40);
display(head);
head=deletion(head);
display(head);
return 0;
}
Question: Adding node between two nodes
in linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node node;
void inserttail(node **head,node **last,int v)
{
node*temp=(node*)malloc(sizeof(node));
temp->info=v;
temp->next=NULL;
if((*head)==NULL)
{
(*head)=temp;
(*last)=temp;
}
else
{
(*last)->next=temp;
(*last)=temp;
}
}
node *insert(node *head,int v)
{
if(head==NULL)
{
head=(node *)malloc(sizeof(node));
head->info=v;
head->next=NULL;
return head;
}
else
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->info=v;
temp->next=head;
head=temp;
}
return head;
}
void insertbet(node* head,int n,int v)
{
node *temp=(node *)malloc(sizeof(node));
if(n==1)
{
temp->next=head;
head=temp;
return;
}
node *curr=head;
for(int i=1;curr!=NULL && i<n-1;i++)
{
temp=temp->next;
}
if(curr=NULL)
{
printf("Invalid position");
free(temp);
return;
}
temp->next=curr->next;
curr->next=temp;
}
void display(node *head)
{
printf("\n");
while(head!=NULL)
{
printf(" %d",head->info);
head=head->next;
}
}
node* deletion(node *head)
{
if(head==NULL)
{
printf("Empty");
return head;
}
node *temp;
temp=head;
head=head->next;
printf("\nDeleted element is %d",temp->info);
return head;
}
int main()
{
node *head = NULL,*last=NULL;
inserttail(&head,&last,10);
inserttail(&head,&last,20);
inserttail(&head,&last,30);
inserttail(&head,&last,40);
display(head);
head=deletion(head);
display(head);
head=deletion(head);
display(head);
int n;
printf("\nEnter the position to enter new node:");
scanf("%d",&n);
insertbet(head,n,50);
printf("new list is:");
display(head);
return 0;
}

You might also like