//See sabse pehle ek khali node bana lena jiska prev and next null ho uske bad use return kar le fir saare operation perform kar lena
//Time taken to make all these < 2.5 hrs (including logic making error and testing)
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node* prev;
int data;
struct node* next;
};
struct node* insBeg(struct node* head, int val) {
struct node* temp = malloc(sizeof(struct node));
temp->prev = NULL;
temp->data = val;
temp->next = NULL;
head = temp;
return (head);
}
void insLeft(struct node** head, int val) {
struct node* temp = malloc(sizeof(struct node));
if(((*head)->next == NULL) && ((*head)->prev == NULL)) {
temp->prev = NULL;
temp->data = val;
temp->next = *head;
(*head)->prev = temp;
*head = temp;
} else {
temp->next = *head;
temp->data = val;
temp->prev = NULL;
(*head)->prev = temp;
*head = temp;
}
}
void insLeftMid(struct node** head, int val, int pos, char prep) {
//Making of Temporary node;
struct node* temp = malloc(sizeof(struct node));
//Making of current and prevous pointer for mid value Insertion
struct node* previous = *head;
struct node* current = *head;
//Calculate total number of nodes;
struct node* for_num_nodes = *head;
int tot_num_nodes = 0;
while(for_num_nodes != NULL) {
tot_num_nodes++;
for_num_nodes = for_num_nodes->next;
}
printf("Total Num of Nodes %d\nNode after mid Insertion\n", tot_num_nodes);
if(pos == 1) {
printf("function had made for this, use that\n");
}else if(pos == tot_num_nodes) {
printf("Insertion after last node, Rukiye jara\n");
} else {
if(prep == 'a') {
int i;
for(i = 1; i<=pos; i++) {
previous = current;
current = current->next;
// printf("%d", i);
}
// printf("pos =%d val = %d", pos, val);
temp->prev = previous;
temp->data = val;
temp->next = current;
current->prev = temp;
previous->next = temp;
} else {
int i;
for(i = 1; i<=pos-2; i++) {
previous = current;
current = current->next;
// printf("%d", i);
}
temp->prev = previous;
temp->data = val;
temp->next = current;
current->prev = temp;
previous->next = temp;
}
}
}
void insRight(struct node** head, int val) {
struct node *temp = malloc(sizeof(struct node));
if(((*head)->next==NULL) && ((*head)->prev==NULL)) {
temp->prev = *head;
temp->data = val;
temp->next = NULL;
(*head)->next = temp;
} else {
//While loop use karenge taaki ham last node ka pata kar sakej;
struct node* chk = *head;
while(chk->next != NULL) {
chk = chk->next;
}
chk->next = temp;
temp->prev = chk;
temp->data = val;
temp->next = NULL;
}
}
void delLeft(struct node** head, int del_pos) {
//Calculate total number of nodes;
struct node* for_num_nodes = *head;
struct node* temp_del = *head;
int tot_num_nodes = 0;
while(for_num_nodes != NULL) {
tot_num_nodes++;
for_num_nodes = for_num_nodes->next;
}
if(*head == NULL) {
printf("node is Empty");
} else if(del_pos == 1) {
struct node* temp_del = *head;
*head = (*head)->next;
free(temp_del);
temp_del = NULL;
} else if(del_pos == tot_num_nodes){
struct node* chk = *head;
while(chk->next != NULL) {
chk = chk->next;
}
chk->prev->next = NULL;
free(chk);
} else {
struct node* to_find = *head;
struct node* del_node = NULL;
struct node* prv = *head;
int i;
for(i = 1; i<=del_pos-1; i++) {
prv = to_find;
to_find = to_find->next;
}
del_node = to_find;
prv->next = to_find->next;
to_find->next->prev = to_find->prev;
free(to_find);
}
}
void printAll(struct node* head) {
struct node* p = head;
while(p != NULL) {
printf("%d->", p->data);
p = p->next;
}
}
void search(struct node** head, int val) {
struct node* to_search = *head;
int nums = 0;
while(to_search != NULL) {
if(to_search->data == val) {
nums = 1;
break;
} else {
to_search = to_search->next;
nums = 0;
}
}
if(nums == 1)
printf("\nData is present");
else
printf("\nsorry no such data exists");
}
int main() {
struct node* head = NULL;
struct node* empty = insBeg(head, 0);
//printf("%d", take->data);
insLeft(&empty, 1);
insLeft(&empty, 2);
insLeft(&empty, 3);
insLeft(&empty, 4);
insLeft(&empty, 5);
//printAll(take);
//insRight(&empty, 1);
//insRight(&empty, 2);
//insRight(&empty, 3);
//printAll(empty);
printAll(empty);
/*Insert after any given postion, isko ham insLeft() function ki help se karenge
mean left mein already kuch elements bhare hong;*/
//insLeftMid(&empty, 9, 4, 'a');
//Deliting nodes
delLeft(&empty, 6);
printf("\n");
printAll(empty);
search(&empty, 6);
return 0;
}
Time taken to make all these < 2.5 hrs (including logic making error and testing)
ReplyDelete