Data Structures Practical 3rd Sem
Data Structures Practical 3rd Sem
if(head==NULL)
{
head=temp;
}
else
{
struct node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("empty list!!");
}
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\n");
temp=temp->next;
}
}
}
void maxelement()
{
int max=head->data;
while(head!=NULL)
{
if(head->data>max)
{
max=head->data;
}
head=head->next;
}
printf("max element is: %d ",max);
}
void minelement()
{
int min=0;
while(head!=NULL)
{
if(min>head->data)
{
min=head->data;
}
head=head->next;
}
printf("\nmin element is: %d",min);
}
void main()
{
clrscr();
int size;
printf("size:");
scanf("%d",&size);
for(int i=0;i<size;i++)
{
append();
}
printf("\nlist is:\n");
display();
maxelement();
minelement();
getch();
}
Q4. Write a function to delete a given list.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
head=temp;
}
else
{
struct node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
void del(int s)
{
struct node *temp,*p,*q;
temp=(struct node*)malloc(sizeof(struct node));
int loc;
printf("enter location to be deleted");
scanf("%d",&loc);
if(loc>s)
{
printf("invalid location");
}
else
if(loc==1)
{
temp=head;
head=temp->next;
temp->next=NULL;
free(temp);
}
else
{
p=head;
int i=1;
while(i<loc-1)
{
p=p->next;
i++;
}
q=p->next;
p->next=q->next;
q->next=NULL;
free(q);
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("empty list!!");
}
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\n");
temp=temp->next;
}
}
}
void main()
{
clrscr();
int size;
printf("size:");
scanf("%d",&size);
for(int i=0;i<size;i++)
{
append();
}
del(size);
printf("\nnew list:\n");
display();
getch();
}
Q5. Write a program to reverse a linked list with iterative and recursive solution.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int data;
struct Node* next;
};
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;
first = *head_ref;
rest = first->next;
if (rest == NULL)
return;
recursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}
void main()
{
clrscr();
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
push(&head2, 16);
push(&head2, 34);
push(&head2, 25);
push(&head2, 7);
printf("\n\nGiven linked list\n");
printList(head2);
recursiveReverse(&head2);
printf("\nReversed Linked list \n");
printList(head2);
getche();
}
Q6. Write a program to reverse a linked list using atmost two extra pointers.
#include <stdio.h>
#include <conio.h>
struct Node {
int data;
struct Node* next;
};
void main()
{
clrscr();
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
}
Q7. Write a program given a linked list and two integers M and N, deleting N nodes after
M nodes.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
if (curr == NULL)
return;
t = curr->next;
for (count = 1; count<=N && t!= NULL; count++)
{
struct node *temp = t;
t = t->next;
free(temp);
}
curr->next = t;
curr = t;
}
}
void main()
{
skipMdeleteN(head, M, N);
}
Q8. Write a program given a linked list, add 1 to it and create a new linked list.
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
Node* next;
};
Node *newNode(int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
if (head == NULL)
return 1;
return head;
}
void printList(Node *node)
{
while (node != NULL)
{
printf("%d", node->data);
node = node->next;
}
printf("\n");
}
int main(void)
{
Node *head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
printf("List is ");
printList(head);
head = addOne(head);
return 0;
}