Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions Double Linked List Primitive operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
struct node* prev;
};

struct node* start = NULL;

void InsertAnyPos(){
int i, pos, c=0;
struct node *temp, *nw;
printf("Enter the position where you want to insert\n");
scanf("%d", &pos);
for(temp = start; temp!=NULL; temp = temp -> next)
c++;
if(pos>c+1)
printf("Invalid Position");
else{
nw= (struct node *)malloc(sizeof(struct node));
if(nw==NULL)
printf("Can't allocate memory\n");
else{
nw->next=NULL;
nw->prev=NULL;
printf("Enter the info part: ");
scanf("%d", &nw->info);
if(pos==1){
if(start!=NULL){
nw->next=start;
start->prev=nw;
}
start=nw;
}
else if(pos==c+1){
for(temp=start; temp->next!=NULL; temp=temp->next);
temp->next=nw;
nw->prev=temp;
}
else{
for(temp = start, i=1; i<pos-1; temp=temp->next, i++)
nw->next = temp->next;
(temp->next)->prev=nw;
nw->prev=temp;
temp->next=nw;
}
}
}
}

void traverse()
{
struct node* temp;

if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->info);
temp = temp->next;
}
}
}
void deleteFirst(){
struct node *temp;
if(start==NULL)
printf("List is empty: Underflow\n");
else if(start->next==NULL){
temp=start;
start=NULL;
free(temp);
printf("\nFirst node deleted successfully\n");
}
else{
temp=start;
start = start->next;
(temp->next)->prev=NULL;
temp->next=NULL;
free(temp);
printf("\nFirst node deleted successfully\n");

}
}

void deleteLast(){
struct node *temp;
if(start==NULL)
printf("List is empty: Underflow\n");
else if(start->next==NULL){
temp=start;
start=NULL;
free(temp);
printf("\nLast node deleted successfully\n");
}
else{
for(temp=start; temp->next!=NULL; temp=temp->next);
(temp->prev)->next=NULL;
temp->prev=NULL;
free(temp);
printf("\nLast node deleted successfully\n");

}
}

void deleteAnyPos(){
int i, pos, c=0;
struct node *temp;
if(start==NULL){
printf("Underflow");
}
else{
printf("Enter the position where you want to insert\n");
scanf("%d", &pos);
}
for(temp = start; temp!=NULL; temp = temp -> next)
c++;
if(pos>c+1)
printf("Invalid Position");
else if(pos==1){
deleteFirst();
}
else if(pos==c+1){
deleteLast();
}
else{
for(i=1, temp=start; i<pos; temp=temp->next, i++);
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
temp->prev=NULL;
temp->next=NULL;
free(temp);
}
}

int main()
{
int choice;
while (1) {
printf("\n1 To print the list\n");
printf("2 For insertion at any position\n");
printf("3 For Deletion of First Node\n");
printf("4 For Deletion of Last Node\n");
printf("5 For Deletion at Any Position\n");
printf("8 Exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
InsertAnyPos();
break;
case 3:
deleteFirst();
break;
case 4:
deleteLast();
break;
case 5:
deleteAnyPos();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}



23 changes: 23 additions & 0 deletions delt_unsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<stdio.h>
int main()
{
int n,i,pos;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("enter the position for deleted element");
scanf("%d",&pos);
n--;
for(i=pos-1;i<n;i++)
{
arr[i] = arr[i+1];
}
for(i=0;i<n;i++)
{
printf("%d \n",arr[i]);
}
}

40 changes: 40 additions & 0 deletions differ.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{

int n,flag,k=0,i,n;
printf("enter the value of n");
scanf("%d",&n);
printf("enter set A \n");
int arr1[n],arr2[n],arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
printf("enter set B \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr2[i]);
}
for( i=0;i<n;i++)
{
flag =0;
for (int j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
flag = 1;
}
if (flag == 0)
{
arr[k]=arr1[i];
k++;
}
}
printf("set A - B equals \n");
for(i=0;i<k;i++)
{
printf("%d \n",arr[i]);
}

}
26 changes: 26 additions & 0 deletions lv2-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
int main()
{
int n,i,sum1=0,sum2=0;
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if (arr[i]%2==0)
{
sum1+=arr[i];
}
else
{
sum2+=arr[i];
}
}
printf("%d %d",sum1,sum2);

return 0;
}
bcxgdshsrttr