From b653a09d778ce63b46bfc22a3c7d1ba19800474c Mon Sep 17 00:00:00 2001 From: amanydv72 <89766177+amanydv72@users.noreply.github.com> Date: Mon, 17 Oct 2022 23:44:57 +0530 Subject: [PATCH] All Opeartions of Linked list in c Please accept my PR with all necessary hacktoberfest labels Signed-off-by: amanydv72 <89766177+amanydv72@users.noreply.github.com> --- Linked List/linked_list.c | 319 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 Linked List/linked_list.c diff --git a/Linked List/linked_list.c b/Linked List/linked_list.c new file mode 100644 index 0000000..f272376 --- /dev/null +++ b/Linked List/linked_list.c @@ -0,0 +1,319 @@ +#include +#include +struct node +{ + int data; + struct node *next; +}*head=NULL; + +void create_linked_list(int a[],int n) +{ + struct node *last,*temp; + head=(struct node *)malloc(sizeof(struct node)); + head->data=a[0]; + head->next=NULL; + last=head; + for(int i=1;idata=a[i]; + temp->next=NULL; + last->next=temp; + last=temp; + } +} + +void display() +{ + struct node *p=head; + while(p) + { + printf("%d ",p->data); + p=p->next; + } + printf("\n"); +} + +int count() +{ + struct node *p=head; + int c=0; + while(p) + { + c++; + p=p->next; + } + return c; +} + +void reverse_display() +{ + struct node *p=head; + int n=count(),i=0; + int a[n]; + while(p) + { + a[i++]=p->data; + p=p->next; + } + for(i=n-1;i>=0;i--) + printf("%d ",a[i]); + printf("\n"); +} + +void recursive_display(struct node *p) +{ + if(p!=NULL) + { + printf("%d ",p->data); + return recursive_display(p->next); + } + printf("\n"); +} + +void reverse_recursive_dispaly(struct node *p) +{ + if(p!=NULL) + { + reverse_recursive_dispaly(p->next); + printf("%d ",p->data); + } +} + +int sum() +{ + int s=0; + struct node *p=head; + while(p) + { + s+=p->data; + p=p->next; + } + return s; +} + +int max() +{ + int max=0; + struct node *p=head; + while(p) + { + if(maxdata) + max=p->data; + p=p->next; + } + return max; +} + +struct node * searching(int key) +{ + struct node*p=head; + while(p) + { + if(key==p->data) + return p; + p=p->next; + } + return NULL; +} + +struct node * Improved_searching(int key) +{ + struct node *p=head,*q=NULL; + while(p) + { + if(key==p->data) + { + q->next=p->next; + p->next=head; + head=p; + + return p; + } + q=p; + p=p->next; + } + return NULL; +} + +void Insert_BFN(int n) +{ + struct node *p; + p=(struct node *)malloc(sizeof(struct node )); + p->data=n; + p->next=head; + head=p; +} + +void Insert_AGP(int n,int pos) +{ + struct node *p,*t=head; + p=(struct node*) malloc(sizeof(struct node)); + p->data=n; + for(int i=1;inext; + } + p->next=t->next; + t->next=p; +} + +void Insert(int n,int pos) +{ + struct node *p=head,*t=NULL; + t=(struct node *)malloc(sizeof(struct node)); + t->data=n; + // t->next=NULL; + if(pos<0 || pos>count()) + return ; + else + { + if(pos==0) + { + t->next=head; + head=t; + } + else + { + for(int i=1;inext; + t->next=p->next; + p->next=t; + } + } +} + +void Insert_last(int n) +{ + struct node *p,*last; + p=(struct node *)malloc(sizeof(struct node)); + p->data=n; + p->next=NULL; + if(head==NULL) + { + head=p; + last=p; + } + else + { + last->next=p; + last=p; + } +} + +void Sorted_insert(int n) +{ + struct node *t,*p=head,*q=NULL; + t=(struct node*)malloc(sizeof(struct node)); + t->data=n; + t->next=NULL; + if(head==NULL) + { + head=t; + } + else + { + while(p->data<=n) + { + q=p; + p=p->next; + } + if(p==head) + { + t->next=head; + head=t; + } + else + { + t->next=q->next; + q->next=t; + } + + } +} + +int Delete_FN() +{ + int n; + struct node *t=head; + head=t->next; + n=t->data; + free(t); + return n; +} + +int Delete_AGP(int pos) +{ + int n; + if(pos<1 || pos>count()) + return -1; + else + { + struct node *p=head,*q=NULL; + for(int i=1;inext; + } + n=p->data; + q->next=p->next; + free(p); + return n; + } +} + +int delete(int pos) +{ + int n=-1; + if(pos<1 || pos>count()) + return n; + else{ + struct node *p=head,*q=NULL; + if(pos==1) + { + n=head->data; + head=head->next; + free(p); + } + else + { + for(int i=1;inext; + } + n=p->data; + q->next=p->next; + free(p); + } + return n; + + } +} + +int main() +{ + int a[5]={1,2,3,4,5}; + // create_linked_list(a,5); + // display(); + // reverse_display(); + // recursive_display(head); + // reverse_recursive_dispaly(head); + // printf("\n%d",sum()); + // printf("\n%d\n",max()); + // printf("\n%d",searching(4)); + // printf("\n%d\n",Improved_searching(4)); + // Insert_BFN(9); + // Insert_AGP(7,5); + // Insert(8,5); + Insert_last(10); + Insert_last(22); + Insert_last(30); + Insert_last(47); + Insert_last(53); + Sorted_insert(4); + display(); + // Delete_FN(); + printf("\n%d \n",delete(3)); + display(); + return 0; +} \ No newline at end of file