sanpai

Data_structures_rev

May 9th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. Linked list reverse
  2.  
  3. struct node * reverse(struct node * head){
  4.  
  5. struct node *prev = NULL,*cur,*next;
  6.  
  7. cur = head;
  8.  
  9. while(cur!=NULL){
  10.  
  11. next=cur ->next;
  12. cur->next= prev;
  13. prev = cur ;
  14. cur = next;
  15.  
  16. }
  17.  
  18. //return head
  19. return prev;
  20.  
  21.  
  22. }
  23. ---------------------------------------------------------------------
  24.  
  25. rotate a linked list
  26.  
  27.  
  28. struct node * reverse(struct node * head , int rol){
  29.  
  30. int i = 0;
  31. struct node * nhead, *ntail, *tail , *temphead = head;
  32.  
  33.  
  34. while (i < rol) {
  35.  
  36. head = head ->next;
  37.  
  38. }
  39.  
  40. ntail = head;
  41.  
  42. nhead = head ->next;
  43.  
  44. while (head->next ! = NULL) {
  45.  
  46.  
  47. head = head - >next ;
  48. }
  49.  
  50. head -> next = temphead;
  51. ntail->next = NULL;
  52.  
  53.  
  54. //return head
  55. return nhead ;
  56.  
  57.  
  58. }
  59. -----------------------------------------------------------------------------------
  60.  
  61. Add two linked lists
  62.  
  63. struct node * Add ( struct node * first, struct node * second ) {
  64.  
  65.  
  66. int sum = 0 ,carry = 0;
  67.  
  68. struct node *new ,*head = null,prev;
  69.  
  70.  
  71. while(first != null || second != null) {
  72.  
  73.  
  74. sum = carry + (if(first)?first->data : 0) + (if(second)?second ->data:0);
  75.  
  76. carry = if(sum > = 0) ? 1 : 0;
  77.  
  78. sum = sum % 10;
  79.  
  80. new = (struct node *) malloc (sizeof(struct node));
  81.  
  82. if (!head)
  83. {
  84. new->data = sum;
  85. new->next = null;
  86. head = new;
  87.  
  88. }
  89. else
  90. {
  91. new->data= sum;
  92. new->next = null;
  93.  
  94. prev ->next= new
  95. }
  96.  
  97. prev = new ;
  98.  
  99. first = first->next;
  100. second = second -> next ;
  101.  
  102. }
  103.  
  104. if (carry > 0 ){
  105.  
  106. new = (struct node *) malloc (sizeof(struct node));
  107. new->data = sum;
  108. new->next = null;
  109. prev ->next= new ;
  110. }
  111. return head;
  112.  
  113. }
  114.  
  115.  
  116. --------------------------------------------------------------------------
  117.  
  118. Generic linked list
  119.  
  120.  
  121. struct node {
  122.  
  123. void *data;
  124. struct node * next;
  125.  
  126. };
  127.  
  128.  
  129. void push( struct node **headref , void * data , int size){
  130.  
  131.  
  132.  
  133. struct node *new;
  134.  
  135. new = (struct node * ) malloc(sizeof(struct node ));
  136. new->data= malloc(size);
  137.  
  138. new->data= *headref;
  139.  
  140. for(i=0;i<size;i++)
  141. *(char *)(new->data + i ) = *(char *)(data + i );
  142.  
  143.  
  144. *headref = new ;
  145.  
  146. }
  147.  
  148. ------------------------------------------------------------------------------------------
  149.  
  150. Tree
  151.  
  152. struct node {
  153.  
  154. int data;
  155.  
  156. struct node *left;
  157. struct node *right;
  158. };
  159.  
  160. properties of BT
  161.  
  162.  
  163. 1) Max number of nodes in a level = 2^(level - 1) .
  164.  
  165. 2) Max number of nodes in a BT of given height = 2^(height) - 1.
  166.  
  167. 3) For a given set of nodes N , the min height = log(N+1) (log to base 2 )
  168.  
  169. ----------------------------------------------------------------------------------------------
  170.  
  171. Inorder traversal
  172.  
  173. left -> root -> right
  174.  
  175.  
  176. void inorder(struct node *head){
  177.  
  178. if (head == NULL)
  179. return;
  180.  
  181. inorder(head->left);
  182.  
  183. printf("%d ",head->data);
  184.  
  185. inorder(head->right);
  186.  
  187. }
  188.  
  189.  
  190. preorder
  191.  
  192. root ->left->right
  193.  
  194.  
  195.  
  196. void preorder(struct node *head){
  197.  
  198. if (head == NULL)
  199. return;
  200.  
  201. printf("%d ",head->data);
  202. preorder(head->left);
  203. preorder(head->right);
  204.  
  205. }
  206.  
  207. postorder
  208.  
  209. left->right->root
  210.  
  211.  
  212. void postorder(struct node *head){
  213.  
  214. if (head == NULL)
  215. return;
  216.  
  217.  
  218. postorder(head->left);
  219. postorder(head->right);
  220. printf("%d ",head->data);
  221.  
  222. }
  223. -----------------------------------------------------------------------------------------------------------------------
  224.  
  225. Reverse string using stack
  226.  
  227.  
  228. struct Stack {
  229.  
  230. int top;
  231. int capacity;
  232. char *array;
  233.  
  234. };
  235.  
  236.  
  237. struct stack* Createstack(int len){
  238.  
  239. struct Stack *stack = (Stack *) malloc (sizeof(struct stack));
  240.  
  241. stack->top = -1;
  242. stack -> capacity = len + 1;
  243.  
  244. stack->array = (char *)malloc(sizeof(char) * len);
  245.  
  246. return stack;
  247.  
  248.  
  249. }
  250.  
  251.  
  252. int Isfull(struct Stack *stack){
  253.  
  254. return stack->top==stack->capacity -1 ;
  255.  
  256. }
  257.  
  258.  
  259. int Isempty(struct Stack *stack){
  260.  
  261. return stack->top== -1 ;
  262.  
  263. }
  264.  
  265.  
  266. void push(struct Stack *stack,char ch) {
  267.  
  268. if(Isfull(stack))
  269. return;
  270.  
  271. stack->array[++(stack->top)] = ch;
  272.  
  273. }
  274.  
  275. char pop(struct Stack *stack) {
  276.  
  277. if(Isempty(stack))
  278. return;
  279.  
  280. return stack->array[(stack->top)--] ;
  281.  
  282. }
  283. void reverse(char str[]) {
  284.  
  285.  
  286. int len = strlen(str);
  287.  
  288. struct node *stack = Createstack(str);
  289.  
  290. for(i = 0 ; i< len ; i++)
  291. push(stack,str[i]);
  292.  
  293. for(i =0 ;i <len ; i ++)
  294. str[i]=pop(stack);
  295.  
  296. }
  297. ---------------------------------------------------------------------------------------------------
  298. reverse string
  299.  
  300.  
  301.  
  302. void swap(char *in1,char * in2){
  303.  
  304. char temp = *in1;
  305.  
  306. *in1 = *in2;
  307.  
  308. *in2 = temp;
  309.  
  310.  
  311. }
  312.  
  313. void reverse(char in[]){
  314.  
  315. int len = strlen(in);
  316.  
  317. for(i=0 ; i< len/2 ; i++)
  318. swap(&in[i],&in[len-i-1]);
  319.  
  320.  
  321. }
  322.  
  323. --------------------------------------------------------------------------------------------------------
  324.  
  325. Queue using two stacks
  326.  
  327.  
  328. struct stack {
  329.  
  330. int data;
  331. struct stack *next;
  332.  
  333. };
  334.  
  335.  
  336. struct queue {
  337.  
  338. struct stack *stack1;
  339. struct stack *stack2;
  340.  
  341. };
  342.  
  343.  
  344. void push(struct stack **top, int data ){
  345.  
  346. struct stack * node = (struct stack *) malloc (sizeof(struct stack));
  347. node->data = data;
  348. node->next=*top;
  349. *top = node;
  350.  
  351. }
  352.  
  353. int pop(struct stack **top) {
  354.  
  355. struct stack *temp = *top;
  356. int data = temp->data;
  357. *top= temp->next;
  358. free(temp);
  359. return data;
  360. }
  361.  
  362.  
  363. void enqueue (struct queue *q, int data ){
  364.  
  365. push(&q->stack1,data);
  366.  
  367.  
  368. }
  369.  
  370. int dequeue(struct queue *q) {
  371.  
  372. int x ;
  373. if(q->stack1 == NULL || q->stack2 == NULL) {
  374.  
  375. printf("Queue is empty ");
  376. return NULL;
  377. }
  378.  
  379. if(q->stack2 == NULL) {
  380.  
  381. while(q->stack1 ! = NULL){
  382.  
  383. x = pop(&q->stack1);
  384. push(&q->stack2,x);
  385.  
  386. }
  387.  
  388. }
  389.  
  390. x = pop(&q->stack2);
  391. return x ;
  392. }
Add Comment
Please, Sign In to add comment