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

Chandan 20238005 Assign 6

Python assignment
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)
6 views9 pages

Chandan 20238005 Assign 6

Python assignment
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

Chandan Kumar

20238005
ECM branch
Assignment 06
Q1. Given an array of integers, find the first repeating element in it. We
need to find the element that occurs more than once and whose index of
first occurrence is smallest.
Code: #include<stdio.h>
int main(){
int num;
printf("Enter the number of elements in array: ");
scanf("%d",&num);
int a,b=__INT16_MAX__;
int arr[num];
for(int i=0;i<num;i++){
printf("Enter the %d index element: ",i+1);
scanf("%d",&arr[i]);
for(int j=0;j<i;j++){
if(arr[i]==arr[j] && j<b){
a=arr[i];
b=j;
}
}
}
for(int i=0;i<num;i++){
printf("%d ",arr[i]);
}
printf("\nThe repeating element is %d and it first occured at %d
position.",a,b+1);
}

Q2. Write a C program that traverses a doubly linked list of positive &
negative integers and deletes all nodes whose keys are negative.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int val;
struct Node* prev;
struct Node* next;
};

struct Node* create(struct Node* head,int num){


int i=0;
struct Node* temp=head;
while(i<num){
struct Node* newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->next=NULL;
newnode->prev=NULL;
printf("Enter the %d index of D. linked list: ", i+1);
scanf("%d",&newnode->val);
if(head==NULL){
head=newnode;
temp=head;
}
else{
temp->next=newnode;
newnode->prev=temp;
temp=temp->next;
}

i++;
}
return head;
}

void display(struct Node* head){


printf("\nThe D. linked listed is: ");
if(head==NULL){
printf("empty list!!");
return;
}
struct Node* temp=head;
while(temp!=NULL){
printf("<-%d->",temp->val);
temp=temp->next;
}
}

struct Node* removeNeg(struct Node* head)


{ if(head==NULL){
return head;
}
while(head && head->val<0){
struct Node* del=head;
head=head->next;
head->prev=NULL;
free(del);
}
struct Node* temp=head;
while(temp->next!=NULL){
if(temp->next->val<0){
struct Node* del=temp->next;
temp->next=del->next;
if(del->next!=NULL){
del->next->prev=temp;}
free(del);
continue;
}
temp=temp->next;
}
return head;
}
int main(){
struct Node* head=NULL;
int num;
printf("Enter the Number of nodes in D.linked list: ");
scanf("%d",&num);
head=create(head,num);
display(head);
head=removeNeg(head);
display(head);
return 0;

Q3. You are given an unsorted array with both positive and negative
elements. You have to find the smallest positive number missing from the
array. Examples
CODE:
#include<stdio.h>
int main(){
printf("Enter the Number of element in array: ");
int num;
scanf("%d",&num);
int arr[num];
int i,max=1;
for(i=0;i<num;i++){
scanf("%d",&arr[i]);
int k=i-1;
int l=arr[i];
while(arr[k]>l&&k>=0){
arr[k+1]=arr[k];
k--;
}
arr[k+1]=l;
}
for(int i=0;i<num;i++) printf("%d ",arr[i]);
i=0;
while(arr[i]<=0&&i<num) i++;
if(i==num){
printf("%d",1);
return 0;
}
int k=1;
while(i<num){
if(k<arr[i]){
printf("\n The smallest positive missing number is %d.",k);
return 0;
}
else if(k==arr[i]){
k++;
i++;
}
else{
i++;
}
}
if(i==num){
printf("\n The smallest positive missing number is %d.",k);
}
return 0;
}
Q4. Write a C program to create a linked list of size at least 5 and
increment the data part of every node present in a linked list by 100.
Display the data both before and after increment.
Code: #include<stdio.h>
#include<stdlib.h>
struct Node{
int val;
struct Node* next;
};

struct Node* create(int num){


struct Node* head=NULL;
struct Node* temp=NULL;
int i=0;
for(;i<num;i++){
struct Node* node=(struct Node*)malloc(sizeof(struct Node));//////
node->next=NULL;
int a;
printf("Enter the %d index element node: ",i+1);
scanf("%d",&a);
node->val=a;
if(head==NULL){
head=node;
temp=head;}
else{
temp->next=node;
temp=temp->next;
}
}
return head;
}
void display(struct Node* head){
struct Node* temp=head;
while(temp!=NULL){
printf("%d->",temp->val);
temp=temp->next;
}
}

struct Node* increment(struct Node* head){


struct Node* temp=head;
while(temp!=NULL){
temp->val=temp->val+100;
temp=temp->next;
}
return head;
}
int main(){
printf("Enter the number of nodes in linked list: ");
int num;
scanf("%d", &num);
struct Node* head=create(num);
printf("\nThe Input Linked list is: ");
display(head);
head=increment(head);
printf("\nAfter increment, The Linked list is: ");
display(head);
}

You might also like