Posts

Showing posts with the label Data structure

Doubly Linklist | Data Structure | C Program

Image
Doubly Link-list: INSERT AT BEGINNING: struct node *temp1,*temp2; temp1=(struct node*)malloc(sizeof(struct node)); printf("Enter value to insert:\n"); scanf("%d",&temp1->info); temp1->next=NULL; temp1->pre=NULL; if(start==NULL) start=temp1; else { temp1->next=start; start->pre=temp1; start=temp1; } INSERT AT GIVEN POSITION: int i,pos; struct node *temp1,*temp2; temp1=(struct node*)malloc(sizeof(struct node)); printf("Enter the postion:\n"); scanf("%d",&pos); printf("Enter value:\n"); scanf("%d",&temp1->info); temp1->next=NULL; temp1->pre=NULL; if(start==NULL) start=temp1; else if(pos==1) { temp1->next=start; start->pre=temp1; start=temp1; } else { temp2=start; for(i=1;i<pos-1;i++) temp2=temp2->next; temp1->next=temp2->next; temp2->next=temp1; temp1->pre=temp2; temp1->next->pre=temp1; } INSERT AT END: struct node...

Circular Linklist | Data Structure | C Program

Image
Circular link list : Code: INSERT AT BEGINNING: struct node *temp1,*temp2; temp1=(struct node*)malloc(sizeof(struct node)); temp2=start; printf("Enter value insert:\t"); scanf("%d",&temp1->info); if(start==NULL) { start=temp1; temp1->link=start; } else { while(temp2->link!=start) temp2=temp2->link; temp2->link=temp1; temp1->link=start; start=temp1; } } INSERT AT GIVEN POSITION: void cr_mid() { int i,pos; struct node *temp1,*temp2; temp2=start; temp1=(struct node*)malloc(sizeof(struct node)); printf("Enter the postion:\n"); scanf("%d",&pos); printf("Enter value to insert:\t"); scanf("%d",&temp1->info); for(i=1;i<pos-1;i++) temp2=temp2->link; temp1->link=temp2->link; temp2->link=temp1; } INSERT AT END: void cr_end() { struct node *temp1,*temp2; temp1=(struct node*)malloc(sizeof(struct node)); temp2=start; printf("Enter value to insert:\n...

SINGLY Linked List | Data Structure | C Program

Image
Singly link list : Code: INSERT AT BEGINNING: in_beg() { struct node *temp; int item; temp=(struct node*)malloc(sizeof(struct node)); printf("Enter a value :\n"); scanf("%d",&item); temp->info=item; temp->link=start; start=temp; } INSERT AT GIVEN POSITION: void de_mid() { struct node *temp1,*temp2; int i,pos; if(start==NULL) printf("List is empty!!\n"); else { temp1=start; printf("Enter the postion:\t"); scanf("%d",&pos); for(i=1;i<=pos-1;i++) temp2=temp1; temp1=temp1->link; temp2->link=temp1->link; free(temp1); } } INSERT AT END: void in_end() { struct node *temp1,*temp2; int item; temp1=(struct node*)malloc(sizeof(struct node)); printf("Enter a number:\t"); scanf("%d",&item); temp2=start; temp1->info=item; temp1->link=NULL; if(start==NULL) start=temp1; else { while(temp2->link!=NULL) temp2=temp2->...

Dequeue | Data Structure | C Program

Image
Dequeue: Code: #include<stdio.h> #define MAX 5 int rear=-1; int front=-1; int cq[MAX]; void inf(); void inr(); void def(); void der(); void di(); void main() { int ch; while(1) { printf("\nChoose any option\n1.Insert from front \t2.Insert from rear\n3.Delete from front\t4.Delete from rear\n5.Display\n"); scanf("%d",&ch); switch(ch) { case 1: inf(); break; case 2: inr(); break; case 3: def(); break; case 4: der(); break; case 5: di(); break; } } } void inf() { int item; if((front==0 && rear==MAX-1) || front==rear+1) printf("Queue is full\n"); else { printf("Enter a value\n"); scanf("%d",&item); if(front==-1) { front=0; rear=0; } else if(front==0) { front=MAX-1; } else front=front-1; cq[front]=item; } } void der() { int item; if(rear==-1) printf("Queue Underflow!!\n"); else { item=c...

Circular Queue | Data Structure | C Program

Image
Circular Queue : Code: #include<stdio.h> #define MAX 5 int rear=-1; int front=-1; int cq[MAX]; void in(); void de(); void di(); void main() { int ch; while(1) { printf("\nChoose any option\n1.Insert\t2.Delete\t3.Display\n"); scanf("%d",&ch); switch(ch) { case 1: in(); break; case 2: de(); break; case 3: di(); break; } } } void in() { int item; if((front==0 && rear==MAX-1) || front==rear+1) { printf("Queue is full!!\n"); } else { printf("Enter a value\n"); scanf("%d",&item); if(front==-1) front=0,rear=0; else if(rear==MAX-1) rear=0; else rear=rear+1; cq[rear]=item; } } void de() { int item; if(front==-1) printf("Queue underflow!!\n"); else { item=cq[front]; printf("You deleted %d\n",item); if(rear==front) { rear=-1; front=-1; } else if(front==MAX-1) { front=0; } else { ...

Queue | Data Structure | C Program

Image
Queue: Code: #include<stdio.h> #define MAX 5 int q[MAX]; int front=-1,rear=-1; void in(); void de(); void di(); void main() { int ch; while(1) { printf("Choose any option\n1.Insert\t2.Delete\t3.Display\n"); scanf("%d",&ch); switch(ch) { case 1: in(); break; case 2: de(); break; case 3: di(); break; } } } void in() { int item; if(rear==MAX-1) { printf("Queue is full\n"); } else { if(front==-1) front=0; rear=rear+1; printf("Enter value to insert\n"); scanf("%d",&item); q[rear]=item; } } void de() { int i; if(front==-1 || front>rear) { printf("Queue is empty\n"); for(i=0;i<MAX;i++) printf...

Stack | C Program | Data Structure |

Image
STACK operation in Data structure:    Code:   #include<stdio.h> #define max 5 /*maximum 5 elements*/ int top=-1; int stk[max]; void push(int); void display(); int pop(); int item; void main() { int x; while(1) { printf("Choose any option:\n1.push\n2.pop\n3.Display\n\n TOP=%d \n\n",top+1); scanf("%d",&x); switch(x) { case 1: printf("Insert a value\n"); scanf("%d",&item); push(item); break; case 2: { if(top!=(-1)) { printf("The element you pop is %d\n",pop()); break; } else { printf("Underflow\n"); break; } } case 3: if(top!=(-1)) { printf("The elements are:\n"); display(); break; } else printf("stack is NULL!!\n"); break; default: break; } } } void push(item) { if(max-1==top...