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
300 changes: 19 additions & 281 deletions Aayush Gupta/Linked List in c/doubly_linked_list.c
Original file line number Diff line number Diff line change
@@ -1,288 +1,26 @@
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct DoublyList
{
int data;
struct DoublyList *prev;
struct DoublyList *next;
};
typedef struct DoublyList DoublyList;
void insertAtBeginning (DoublyList **, DoublyList **);
void insertAtEnd (DoublyList **, DoublyList **);
void insertAfterElement(DoublyList **, DoublyList **);
void displayInorder(DoublyList *);
void displayReverseOrder(DoublyList*);
void insertBeforeElement(DoublyList**);
#include <stdio.h>

int main() {

void main()
{
// clrscr();
DoublyList *head = NULL;
DoublyList *tail = NULL;
insertAtBeginning (&head, &tail);
insertAtBeginning (&head, &tail);
insertAtBeginning (&head, &tail);
int marks[10], i, n, sum = 0;
double average;

printf("Enter number of elements: ");
scanf("%d", &n);

displayInorder(head);
for(i=0; i < n; ++i) {
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable
sum += marks[i];
}

// explicitly convert sum to double
// then calculate average
average = (double) sum / n;

insertAtEnd (&head, &tail);
insertAtEnd (&head, &tail);
insertAtEnd (&head, &tail);
printf("Average = %.2lf", average);


displayInorder(head);


insertAfterElement(&head, &tail);
displayInorder(head);


insertBeforeElement(&head);
displayInorder(head);


getch();
}


// Insert at the beginning of the list
void insertAtBeginning (DoublyList **head, DoublyList **tail)
{
int d;
printf("\n Enter the data: ");
scanf("%d", &d);
DoublyList *node =(DoublyList*) malloc (sizeof(DoublyList));
node->data = d;
if(*head==NULL)
{
node->prev=NULL;
node->next = NULL;
*head = *tail = node;
}
else
{
node->prev = NULL;
node->next = *head;
(*head)->prev = node;
*head = node;
}
}
void insertAtEnd (DoublyList **head, DoublyList **tail)
{
int d;
printf("\n Enter the data: ");
scanf("%d", &d);
DoublyList *node = (DoublyList*)malloc(sizeof(DoublyList));
node->data = d;
if(*head == NULL)
{
node->prev = NULL;
node->next = NULL;
*head = *tail = node;
}
else
{
node->next=NULL;
node->prev=*tail;
(*tail)->next=node;
*tail=node;
}
}
void displayInorder(DoublyList *head)
{
printf("\nThe elements in the list are: ");
while(head != NULL)
{
printf("%d\t", head->data);
head = head->next;
}
}


void displayReverseOrder(DoublyList* tail)
{
printf("\nThe elements in the list are: ");
while(tail != NULL)
{
printf("%d\t", tail->data);
tail = tail->prev ;
}
}

DoublyList *search(DoublyList* head, int searchelement)
{
while(head != NULL)
{
if(head->data == searchelement)
return head;
head =head->next;
}
return NULL;
}
// Inserting after a given element
void insertAfterElement (DoublyList **head, DoublyList **tail)
{
DoublyList *node;
DoublyList *loc;
DoublyList *temp = *head;
int d;
int searchelement;
printf("\n Enter the data to be inserted: ");
scanf("%d",&d);
printf("\n Enter the search element after which the node has to be inserted: ");
scanf("%d", &searchelement);




loc=search(temp,searchelement);
node = (DoublyList *)malloc(sizeof(DoublyList));
node->data = d;
if(loc == NULL)
return;
if(loc->next == NULL)
{
node->next = NULL;
loc->next=node;
node->prev=*tail;
*tail=node;
}
else
{
node->prev=loc;
node->next=loc->next;
(loc->next)->prev=node;
loc->next=node;
}
}


void insertBeforeElement(DoublyList **head)
{
int d;
int searchelement;
DoublyList *node, *loc;
DoublyList *temp = *head;
printf("\n Enter the data to be inserted: ");
scanf("%d", &d);
printf("\n Enter the search element after which the node has to be inserted: ");
scanf("%d", &searchelement);
loc=search(temp, searchelement);
if(loc==NULL)
return;
node=(DoublyList*) malloc(sizeof(DoublyList));
node->data=d;
if(loc->prev==NULL)
{
node->prev=NULL;
loc->prev=node;
node->next=*head;
*head=node;
}
else
{
node->prev=loc->prev;
node->next=loc;
(loc->prev)->next = node;
loc->prev=node;
}
}


void deleteAtBegining(DoublyList **head, DoublyList **tail)
{
DoublyList *temp;
if(*head==NULL)
return;
temp = *head;
if(*head == *tail) /*one element only*/
*head=*tail=NULL;
else
{
(temp->next)->prev=NULL;
*head=temp->next;
}
free(temp);
}
void deleteAtEnd(DoublyList **head, DoublyList **tail)
{
DoublyList *temp;
if(*head==NULL)
return;
temp = *tail;
if(*head == *tail) /*one element only*/
*head=*tail=NULL;
else
{
(temp->prev)->next=NULL;
*tail=temp->prev;
}
free(tail);
}
void deleteAfterElement(DoublyList **head, DoublyList **tail)
{
DoublyList *temp, *loc;
temp = *head;
int d;
int searchelement;
printf("\n Enter the data to be inserted: ");
scanf("%d", &d);
printf("\n Enter the search element after which the node has to be deleted: ");
scanf("%d", &searchelement);
loc = search(temp, searchelement);
if(loc==NULL)
return;
else if(loc->next->next==NULL)
{
temp=loc->next;
loc->next=NULL;
*tail=loc;
free(temp);
}
else
{
temp =loc->next;
loc->next=temp->next;
(temp->next)->prev=loc;
free(temp);
}
}
// void deleteAfterelement (node **head, node **tail, int item, int before)
// {
// node *ptr, *loc;
// ptr=head;
// loc=search(ptr,before);
// if(loc==NULL)
// return;
// else if((loc->prev)->prev==NULL)
// {
// ptr=loc->prev;
// loc->prev=NULL;
// *head=loc;
// free(ptr);
// }
// else
// {
// ptr=loc->prev;
// loc->prev=ptr->prev;
// (ptr->prev)->next=loc;
// free(ptr);
// }
// }


// void deletelist(node **head, node **tail)
// {
// node *ptr;
// while(*head!=NULL)
// {
// ptr=*head;
// *head=(*head)->next;
// free(ptr);
// }
// *tail=NULL;
// }
return 0;
}
23 changes: 23 additions & 0 deletions calculate avg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

int main() {

int marks[10], i, n, sum = 0;
double average;

printf("Enter number of elements: ");
scanf("%d", &n);

for(i=0; i < n; ++i) {
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

sum += marks[i];
}

average = (double) sum / n;

printf("Average = %.2lf", average);

return 0;
}
Binary file added calculate avg.exe
Binary file not shown.
Binary file added calculate avg.o
Binary file not shown.