diff --git a/src/Chapter_04_Queues/CircularLinkedListUDF.c b/src/Chapter_04_Queues/CircularLinkedListUDF.c new file mode 100644 index 0000000..b1d914f --- /dev/null +++ b/src/Chapter_04_Queues/CircularLinkedListUDF.c @@ -0,0 +1,188 @@ +#include +#include +struct node +{ +int data; +struct node *next; +}; +typedef struct node NODE; +NODE *LLnode,*head,*temp,*prev,*second; +NODE* insert_at_front(NODE *); +NODE* insert_at_end(NODE *); +NODE* delete_at_front(NODE *); +NODE* delete_at_end(NODE *); +void display(NODE *); + + +main() +{ +int ch; +head=NULL; + +do +{ + printf("1--insert node at the front\n"); + printf("2--insert node at the end\n"); + printf("3--delete a node at the front\n"); + printf("4--delete a node at the end\n"); + printf("5--display the linked list\n"); + printf("6--close the program\n"); + +printf("enter your choice\n"); +scanf("%d",&ch); +switch(ch) +{ +case 1: head=insert_at_front(head); + + break; +case 2: head=insert_at_end(head); + + break; +case 3:head=delete_at_front(head); + + break; +case 4: head=delete_at_end(head); + + break; +case 5: display(head); + break; +case 6: exit(0);break; +default:printf("Invalid choice\n"); + break; +} +}while(ch!=6); + + +} +NODE* insert_at_front(NODE *head) +{ + LLnode=(NODE *)malloc(sizeof(NODE)); + printf("enter the data\n"); + scanf("%d",&LLnode->data); + + if(head==NULL) + { + head=LLnode; + head->next=head; + } + else + { + temp=head; + while(temp->next!=head) + { + temp=temp->next; + } + LLnode->next=head; + head=LLnode; + temp->next=head; + } + return(head); + +} + +NODE* insert_at_end(NODE *head) +{ + LLnode=(NODE *)malloc(sizeof(NODE)); + printf("enter the data\n"); + scanf("%d",&LLnode->data); + + if(head==NULL) + { + head=LLnode; + head->next=head; + } + else + { + temp=head; + while(temp->next!=head) + { + temp=temp->next; + } + temp->next=LLnode; + LLnode->next=head; + } + return(head); + +} + +NODE* delete_at_front(NODE *head) +{ + if(head==NULL) + { + printf("Linked list is empty...cant delete a node\n"); + } + else + { + + if(head->next==head) + { + printf("node deleted=%d\n",head->data); + free(head); + head=NULL; + } + else + { + second=head->next; + temp=head; + while(temp->next!=head) + { + temp=temp->next; + } + printf("node deleted=%d\n",head->data); + free(head); + head=second; + temp->next=head; + } + + } + return(head); +} + +NODE* delete_at_end(NODE *head) +{ + if(head==NULL) + { + printf("Linked list is empty...cant delete a node\n"); + } + else if(head->next==head) + { + printf("node deleted=%d\n",head->data); + free(head); + head=NULL; + } + else + { + temp=head; + while(temp->next!=head) + { + prev=temp; + temp=temp->next; + } + printf("node deleted=%d\n",temp->data); + free(temp); + prev->next=head; + } + return(head); +} + + +void display(NODE *head) +{ + if(head==NULL) + { + printf("Linked list is empty\n"); + } + else + { + printf("the linked list is as below\n"); + temp=head; + while(temp->next!=head) + { + printf("%d->",temp->data); + temp=temp->next; + } + printf("%d->",temp->data); + + printf("\n"); + } +} diff --git a/src/Chapter_04_Stacks/Linked list program(UDF).c b/src/Chapter_04_Stacks/Linked list program(UDF).c new file mode 100644 index 0000000..47fabb4 --- /dev/null +++ b/src/Chapter_04_Stacks/Linked list program(UDF).c @@ -0,0 +1,164 @@ + #include +#include +struct CET +{ +char name[20]; +int marks; +char city[20]; +int phone; +struct CET *next; +}; +typedef struct CET NODE; +NODE *LLnode,*head,*temp,*prev; +NODE* insert_at_front(NODE *); +NODE* insert_at_end(NODE *); +NODE* delete_at_front(NODE *); +NODE* delete_at_end(NODE *); +void display(NODE *); + + +main() +{ +int ch; +head=NULL; + +do +{ + printf("1--insert node at the front\n"); + printf("2--insert node at the end\n"); + printf("3--delete a node at the front\n"); + printf("4--delete a node at the end\n"); + printf("5--display the linked list\n"); + printf("6--close the program\n"); + +printf("enter your choice\n"); +scanf("%d",&ch); +switch(ch) +{ +case 1: head=insert_at_front(head); + + break; +case 2: head=insert_at_end(head); + + break; +case 3:head=delete_at_front(head); + + break; +case 4: head=delete_at_end(head); + + break; +case 5: display(head); + break; +case 6: exit(0);break; +default:printf("Invalid choice\n"); + break; +} +}while(ch!=6); + + +} +NODE* insert_at_front(NODE *head) +{ + LLnode=(NODE *)malloc(sizeof(NODE)); + printf("enter the name, marks, city, phone\n"); + scanf("%s%d%s%d",&LLnode->name,&LLnode->marks,&LLnode->city,&LLnode->phone); + + if(head==NULL) + { + head=LLnode; + head->next=NULL; + } + else + { + LLnode->next=head; + head=LLnode; + } + return(head); + +} + +NODE* insert_at_end(NODE *head) +{ + LLnode=(NODE *)malloc(sizeof(NODE)); + printf("enter the name, marks, city, phone\n"); + scanf("%s%d%s%d",&LLnode->name,&LLnode->marks,&LLnode->city,&LLnode->phone); + LLnode->next=NULL; + if(head==NULL) + { + head=LLnode; + } + else + { + temp=head; + while(temp->next!=NULL) + { + temp=temp->next; + } + temp->next=LLnode; + } + return(head); + +} + +NODE* delete_at_front(NODE *head) +{ + if(head==NULL) + { + printf("Linked list is empty...cant delete a node\n"); + } + else + { + temp=head; + printf("node deleted=%s%d%s%d\n",temp->name,temp->marks,temp->city,temp->phone); + head=head->next; + free(temp); + } + return(head); +} + +NODE* delete_at_end(NODE *head) +{ + if(head==NULL) + { + printf("Linked list is empty...cant delete a node\n"); + } + else if(head->next==NULL) + { + printf("node deleted=%s%d%s%d\n",temp->name,temp->marks,temp->city,temp->phone); + free(head); + head=NULL; + } + else + { + temp=head; + while(temp->next!=NULL) + { + prev=temp; + temp=temp->next; + } + printf("node deleted=%s%d%s%d\n",temp->name,temp->marks,temp->city,temp->phone); + free(temp); + prev->next=NULL; + } + return(head); +} + + +void display(NODE *head) +{ + if(head==NULL) + { + printf("Students data list is empty\n"); + } + else + { + printf("Students information is as below\n"); + temp=head; + while(temp!=NULL) + { + printf("name= %s\nmarks= %d\ncity= %s\nphone= %d\n",temp->name,temp->marks,temp->city,temp->phone); + temp=temp->next; + } + printf("\n"); + } +} diff --git a/src/Chapter_04_Stacks/StacksLinkedListUDF.c b/src/Chapter_04_Stacks/StacksLinkedListUDF.c new file mode 100644 index 0000000..0851d32 --- /dev/null +++ b/src/Chapter_04_Stacks/StacksLinkedListUDF.c @@ -0,0 +1,110 @@ +#include +#include +struct stack +{ +int data; +struct stack *next; +}; +typedef struct stack NODE; +NODE *top,*temp,*LLnode,*nnode; +NODE *push(NODE *); +NODE *pop(NODE*); +void display(NODE*); +NODE *get_node(); +main() +{ +int ch; +top=NULL; +do +{ + printf("1--push\n"); + printf("2--pop\n"); + printf("3--display\n"); + printf("4--exit\n"); + printf("enter the choice\n"); + scanf("%d",&ch); + switch(ch) + { + case 1: top=push(top); + break; + case 2: if(top==NULL) + printf("stack is empty\n"); + else + { + top=pop(top); + } + break; + case 3: display(top);break; + case 4: exit(0);break; + default: printf("Invalid choice\n");break; + } +}while(ch!=4); +} + +NODE *get_node() +{ + nnode=(NODE*)malloc(sizeof(NODE)); + if(nnode==NULL) + { + printf("memory not allocated\n"); + exit(0); + } + else + { + printf("enter the element\n"); + scanf("%d",&nnode->data); + } + return(nnode); + +} + +NODE *push(NODE *top) +{ + LLnode=get_node(); + if(top==NULL) + { + top=LLnode; + LLnode->next=NULL; + } + else + { + LLnode->next=top; + top=LLnode; + } + return(top); + + +} + +NODE *pop(NODE *top) +{ + printf("the element poped=%d\n",top->data); + if(top->next==NULL) + { + free(top); + top=NULL; + } + else + { + temp=top; + top=top->next; + free(temp); + } + return(top); +} + +void display(NODE *top) +{ + if(top==NULL) + printf("stack is empty\n"); + else + { + temp=top; + printf("stack elements are\n"); + while(temp!=NULL) + { + printf("%d\n",temp->data); + temp=temp->next; + } + } +}