//1 linked list #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *addr;//address of next node }; struct node *start;//holds address of first node, initially NULL int empty()//is list empty { if(start==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *cur = start; printf("List: "); while(cur!=NULL) { printf("%d ",cur->val); cur = cur->addr; } printf("\n"); } void ins(int x)//insert a new element in linked list { struct node *temp, *cur, *prev; cur = start; prev = NULL; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->addr = NULL;//make address part as NULL if(start==NULL)//if first node { start=temp; } else//find place for it in sorted linked list { while(cur!=NULL && cur->val <= x) {
42
Embed
1 linked list€¦ · //3 queue using doubly linked list #include #include struct node{//node of queue (FIFO) int val;//value part struct node *next,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
//1 linked list #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *addr;//address of next node }; struct node *start;//holds address of first node, initially NULL int empty()//is list empty { if(start==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *cur = start; printf("List: "); while(cur!=NULL) { printf("%d ",cur->val); cur = cur->addr; } printf("\n"); } void ins(int x)//insert a new element in linked list { struct node *temp, *cur, *prev; cur = start; prev = NULL; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->addr = NULL;//make address part as NULL if(start==NULL)//if first node { start=temp; } else//find place for it in sorted linked list { while(cur!=NULL && cur->val <= x) {
prev = cur; cur = cur->addr; } temp->addr = cur; prev->addr = temp; } traverse(); } void del(int x)//delete a node if present in linked list { struct node *cur, *prev; cur = start; prev = NULL; if(empty())//no element in linked list { printf("Error : list is empty"); } else//atleast 1 element { while(cur!=NULL && cur->val==x)//find a node having value=x from first node to last node { prev = cur; cur = cur->addr; } if(cur==NULL)//not found printf("Error : not found"); else//found, delete it { if(prev==NULL)//first element start = cur->addr; else prev->addr = cur->addr;//non first element traverse(); } } } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 20 30 ins(10);//10 10 20 30 ins(50);//10 10 20 30 50 del(20);//10 10 30 50 del(70);//error : not found del(10);//10 30 50
//2 stack using linked list (doubly) #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *prev;//address of prev node struct node *next;//address of next node }; struct node *top;//holds address of stack top, initially NULL int empty()//is stack empty { if(top==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *x = top; printf("stack:\n"); while(x!=NULL) { printf("%d\n",x->val); x = x->prev; } printf("\n"); } void push(int x)//insert a new element in stack at stack top { struct node *temp; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->prev = NULL;//make address part as NULL temp->next = NULL; if(top==NULL)//if first node { top=temp; } else//find place for it in sorted linked list { top->next = temp;//insert new element on top of stack temp->prev = top;
top = temp;//new element is new stack top } traverse(); } void pop()//remove top most element { struct node *x; if(empty())//no element in stack { printf("Error : stack is empty"); } else//atleast 1 element { if(top->prev!=NULL) (top->prev)->next = NULL; top = top->prev; traverse(); } } int main() { push(10);//10 push(30);//10 30 push(20);//10 30 20 push(10);//10 30 20 10 push(50);//10 30 20 10 50 pop();//10 30 20 10 pop();//10 30 20 pop();//10 30 pop();//10 pop();// pop();//error : stack empty getch(); }
//3 queue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void ins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp; }
//5a input restricted dequeue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted //tail end will also be used to delete an existing element struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void ins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp;
} else { temp->prev = tail; tail->next = temp; tail = temp; } show(); } void hdel()//deleting a node from head end { if(empty()) { printf("Error : queue is empty\n"); } else { head = head->next; if(head!=NULL) head->prev = NULL; show(); } } void tdel()//deleting a node from tail end { if(empty()) { printf("Error : queue is empty\n"); } else { tail = tail->prev; if(tail!=NULL) tail->next = NULL; show(); } } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 30 20 ins(10);//10 30 20 10 hdel();//30 20 10
//5b output restricted dequeue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted //head end can be used to insert a new element too struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void tins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp;
} else { temp->prev = tail; tail->next = temp; tail = temp; } show(); } void hins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp; } else { temp->next = head; head->prev = temp; head = temp; } show(); } void del()//deleting a node from head end { if(empty()) { printf("Error : queue is empty\n"); } else { head = head->next; if(head!=NULL) head->prev = NULL; show(); } } int main() { tins(10);//10
//rotation x->right = y; y->left = z; //update heights y->height = max( getHeight(y->left),getHeight(y->right))+1; x->height = max( getHeight(x->left),getHeight(x->right))+1; //return new root return x; } struct node * leftRotate(struct node *y) { struct node *x = y->right; struct node *z = x->left; //rotation x->left = y; y->right = z; //update heights y->height = max( getHeight(y->left),getHeight(y->right))+1; x->height = max( getHeight(x->left),getHeight(x->right))+1; //return new root return x; } struct node * insert(struct node *x, int val) { int bal; //if AVL tree is empty then current node is root if(x==NULL) return( newNode(val) ); //recursive call to insert() as new node has to be attached as leaf node if( x->key>val )//insert in left subtree x->left = insert(x->left,val); else//insert in right subtree x->right = insert(x->right,val); //update height of ancestor //new height of ancestor will be eqaul to max height among left & right child, +1 //due to recursive call of insert() height of all ancestor will updated x->height = max( getHeight(x->left), getHeight(x->right) )+1; //check balance of this ancestor to find out if it is unbalanced or not
bal = balance(x); //if unbalanced then can fall in one of the following 4 cases //LL case if(bal>1 && val<(x->left)->key) return rightRotate(x); //RR case if(bal<-1 && val>(x->right)->key) return leftRotate(x); //LR case if(bal>1 && val>(x->left)->key) { x->left = leftRotate(x->left); return rightRotate(x); } //RL case if(bal<-1 && val<(x->right)->key) { x->right = rightRotate(x->right); return leftRotate(x); } //if no unbalance then return x; } void preorder(struct node *temp)//preorder traversal on tree { if(temp!=NULL) { printf("%d ",temp->key); preorder(temp->left); preorder(temp->right); } } int main() { root = NULL; root = insert(root,10); root = insert(root,20); root = insert(root,30); root = insert(root,40); root = insert(root,50); root = insert(root,25); preorder(root); }
//9a quick sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } int part(int lo, int hi) { int pivot = data[lo]; int i=lo+1; int j=hi; int t; while(1) { while(data[j]>pivot){ j--; } while(data[i]<pivot){ i++; } if(i<j) { t = data[i]; data[i]=data[j]; data[j]=t; } else { t = data[j]; data[j]=data[lo]; data[lo]=t; return j; } } } void quickSort(int lo, int hi) { int p; if(lo<hi) {
p = part(lo,hi); quickSort(lo,p); quickSort(p+1,hi); } } int main() { quickSort(0,9); show(); return 0; }
//9b merge sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case int temp[10]; void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } void merge(int lo, int mid, int hi) { int x=lo; int y=mid+1; int i; for(i=lo; x<=mid && y<=hi; i++) { if(data[x]<=data[y]) temp[i]=data[x++]; else temp[i]=data[y++]; } while(x<=mid) temp[i++]=data[x++]; while(y<=hi) temp[i++]=data[y++]; for(i=lo; i<=hi; i++) data[i] = temp[i]; } void part(int lo, int hi) { int mid; if(lo<hi) { mid = (lo+hi)/2; part(lo,mid); part(mid+1,hi); merge(lo,mid,hi); } else
return; } int main() { part(0,9); show(); return 0; }
/*6 heap tree del() can be used to create 9c Heap sort
max heap hence descending sort tree using an array (indexing from 0) if a node is at n index then its parent at ceil((n-1)/2) left child at 2n+1 right child at 2n+2 binary tree with m level will have max 2^m-1 nodes */ #include<stdio.h> #include<math.h> #define LEVEL 3 int heap[7]; int next=0;//next position to insert a new element int size; void show() { int i; printf("sorted data: "); for(i=0;i<next;i++) printf("%d ",heap[i]); } void heapify(int n) { int temp; if(n==0)//root does not have parent to compare return; int parent = (int)floor((double)(n-1)/2); if(heap[parent]>heap[n]) return; else { temp = heap[parent]; heap[parent]=heap[n]; heap[n]=temp; heapify(parent); } } void heapifyDown(int n) { int temp; if(n>next)//can not go further last node return; int left = 2*n+1;//location of left child
int right = 2*n+2;//location of right child if( (left<next && heap[left]<heap[n]) && (right<next && heap[right]<heap[n]) )//index does exist && child value is less than parent return;//no further heapify necessary else if(left<next && heap[left]>=heap[right] && heap[left]>heap[n])//left child > parent && left child > right child && left does exist { temp = heap[left];//swap heap[left]=heap[n]; heap[n]=temp; heapifyDown(left);//heapify further down } else if(right<next && heap[right]>=heap[left] && heap[right]>heap[n])//right child > parent && right child > left child && right does exist { temp = heap[right];//swap heap[right]=heap[n]; heap[n]=temp; heapifyDown(right);//heapify further down } } void del() { int temp; next--; printf("%d ",heap[0]);//print root value temp=heap[0];//swap last element with root heap[0]=heap[next]; heap[next]=temp; heapifyDown(0);//heapify in down direction from root to leaf } int main() { int i; size=(int)pow(2,LEVEL)-1;//max nodes that a binary tree can have for(i=0;i<size;i++)//take a data and create/update heap tree { /*printf("Enter value:"); scanf("%d",&heap[next]);*/ heap[next]=i+1; heapify(next); next++; } show();//show heap tree printf("\nSorted data:"); for(i=0;i<size;i++)//delete each node one by one, gives data in heap sorted del();
return 0; }
//9d insertion sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } void insertSort() { int i,j,t; for(i=1; i<=9; i++) { for(j=i; j>0 && data[j]<data[j-1]; j--) { t = data[j]; data[j]=data[j-1]; data[j-1]=t; } } } int main() { insertSort(); show(); return 0; }
//10a graph: bfs //assumed to be connected graph (directed and undirected anyone) //if not connected then nay get stuck #include<stdio.h> int gm[4][4]={ {0,1,0,1}, {1,0,1,0}, {0,1,0,0}, {1,0,0,0} }; char names[4]={'a','b','c','d'}; int visited[4]={0,0,0,0};//0-unvisited & unknown, 1-unvisited & known, 2-visited & known int q[4];//bfs Q of unvisted vertices int top=0;//point to start of Q void addQ(int x) { visited[x]=1; q[top++]=x; } int Qempty() { if(top==0) return 1; else return 0; } void Qremove() { int i; for(i=0;i<top;i++) q[i]=q[i+1]; top--; } void bfs(int x) { int i; printf("%c ",names[x]);//print in BFS path visited[x]=2;//declare as visited & known for(i=0;i<4;i++)//add all unknown neighbors in Q { if(visited[i]==0 && gm[x][i]!=0) addQ(i);
} Qremove();//remove from Q } int main() { int start; printf("BFS path: "); start=0; addQ(start); while(!Qempty()) { bfs(q[0]); } return 0; }
//10b graph: dfs //assumed to be connected graph (directed and undirected anyone) //if not connected then nay get stuck #include<stdio.h> int gm[4][4]={ {0,1,0,1}, {1,0,1,0}, {0,1,0,0}, {1,0,0,0} }; char names[4]={'a','b','c','d'}; int visited[4]={0,0,0,0};//0-unvisited & unknown, 1-unvisited & known, 2-visited & known int stack[4];//dfs stack of unvisted vertices int top=0;//point to start of stack void push(int x) { visited[x]=1; stack[top++]=x; } int empty() { if(top==0) return 1; else return 0; } void pop() { top--; } void dfs(int x) { int i; push(x); for(i=0; i<4; i++) { if(visited[i]==0 && gm[x][i]!=0) dfs(i); } printf("%c ",names[x]); pop(); }
int main() { int start; printf("DFS path: "); start=0; do { dfs(start); }while(!empty()); return 0; }
//11 static list, collection of elements in sorted order #include<stdio.h> #include<stdlib.h> #define SIZE 5 int list[SIZE];//list using an array of fixed size int next=0;//special pointer indicating location to store new element or delete existing int full()//is list full? { if(next==SIZE) return 1; else return 0; } int empty()//is list empty? { if(next==0) return 1; else return 0; } void disp()//show all elements in list { int i; printf("Static List: "); for(i=0; i<next; i++) printf("%d ",list[i]); printf("\n"); } void ins(int x)//insert a new value { int i, j; if(full())//error if list is full { printf("Error : List is full\n"); } else//if not then insert new element in sorted order { for(i=0; i<next && list[i]<x; i++);//find place for new element in list for(j=next; j>i; j--)//shift right list[j] = list[j-1]; list[i] = x; next++; disp();
} } void del(int x)//delete existing value { int i, j; if(empty())//error if list is empty { printf("Error : List is empty"); } else { j = search(x); if(j==next) printf("Error : element to delete not found\n"); else { for(i=j; i<next-1; i++)//left shift list[i] = list[i+1]; next--; disp(); } } } int search(int s)//search if an element is present or not { int i; for(i=0; i<next; i++) { if(list[i]==s)//element found return i; } return i;//element not found } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 20 30 ins(10);//10 10 20 30 ins(50);//10 10 20 30 50 ins(60);//error : full del(20);//10 10 30 50 del(70);//error : not found del(10);//10 30 50 del(10);//30 50
prev = NULL; cur = root; while(cur != NULL) { prev = cur; if(x <= cur->val) cur = cur->left; else cur = cur->right; } temp->parent = prev; if(x<=prev->val) prev->left = temp; else prev->right = temp; } show(root); printf("\n"); } struct node * bst(int x) { struct node *cur; cur = root; while( cur!=NULL && cur->val!=x )//search till found or end of tree { if( x<=cur->val ) cur = cur->left; else cur = cur->right; } //printf("\n~%d~\n",cur); return cur;//if NULL then not found else address of that node*/ } void del(int x) { int newValue; struct node *temp; struct node *next; temp = bst(x); if(temp==NULL) printf("\n%d not found in BST\n",x); else//non root node { if(temp->right==NULL && temp->left==NULL)//leaf node { if(temp==root)//root node is to be deleted root = NULL;
else { if((temp->parent)->left == temp )//left leaf child of parent (temp->parent)->left = NULL; else (temp->parent)->right = NULL;//right leaf child of parent } } else if(temp->right==NULL)//with only left child { if(temp==root)//root node is to be deleted root = temp->left; else { if(( temp->parent)->left == temp )//left non leaf child of parent { (temp->parent)->left = temp->left; (temp->left)->parent = temp->parent; } else//right non leaf child of parent { (temp->parent)->right = temp->left; (temp->left)->parent = temp->parent; } } } else if(temp->left==NULL)//with only right child { if(temp==root)//root node is to be deleted root = temp->right; else { if(( temp->parent)->left == temp )//left non leaf child of parent { (temp->parent)->left = temp->right; (temp->right)->parent = temp->parent; } else//right non leaf child of parent { (temp->parent)->right = temp->right; (temp->right)->parent = temp->parent; } } } else//node with two childern { //find inorder successor of node to be deleted (min in right subtree) next = temp->right;