Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Sunday, June 13, 2010

Mth from the last of Link List in C

1: #include <stdio.h>
2: 
3: typedef struct node
4: {
5:   struct node *next;
6:   int data;
7: }listNode;
8: 
9: //passing the **head so that head can be modified
10: int insert(listNode **head,listNode *node)
11: {
12:   if(*head == NULL)
13:     *head = node;
14:   else
15:   {
16:     node->next = *head;
17:     *head = node;
18:   }    
19:   return 1;
20: }
21: 

Saturday, June 12, 2010

Link List In C++

1: #include <stdio.h>
2: #include <iostream>
3: 
4: using namespace std;
5: 
6: class linkList
7: {
8:   typedef struct node
9:   {
10:     struct node *next;
11:     int data;
12:     node():next(NULL),data(-1){}
13:   }listNode;
14:   listNode *head;
15:   listNode *end;
16:   int m_nSize;
17: public:
18:   linkList(int size = 0);
19:   int insert(int data);
20:   //how many times find is in the list
21:   int count(int find);
22:   int getNth(int index);//index starting from 0
23:   //removes all the nodes, frees and set head null
24:   void deleteList();
25:   //pop the head, changes the head
26:   int pop();
27:   //inser data at nth index
28:   int insertAtNth(int index,int data);
29:   int sortedInsert(int data);
30:   void display();
31: };
32: 


Wednesday, June 9, 2010

Queue in C++, without passing pointers

1: #include <stdio.h>
2: #include <iostream>
3: 
4: #define CAP 10
5: //in case of c++, if there is no need to pass the pointers
6: //of head and tail as they are members of the class itself
7: //and the member functions can access them and modify them
8: class queue
9: {
10:   typedef struct node
11:   {
12:     struct node *next;
13:     int data;
14:   }qNode;
15:   
16:   qNode *head;
17:   qNode *end;
18:   int cap;
19:   int size;
20: public:
21:   queue(int cap = CAP);
22:   int dequeue();
23:   int enqueue(int data);
24:   void show();
25: };
26: 

Tuesday, June 8, 2010

Queue In C++ using link list

1: #include <stdio.h>
2: #include <iostream>
3: 
4: #define CAP 10
5: //if we are passing the pointers then they have to passed by 
6: //reference ie *&
7: 
8: class queue
9: {
10:   typedef struct node
11:   {
12:     struct node *next;
13:     int data;
14:   }qNode;
15:   
16:   qNode *head;
17:   qNode *end;
18:   int cap;
19:   int size;
20: public:
21:   queue(int cap = CAP);
22:   int dequeue(qNode *&head);
23:   int enqueue(qNode *&end, int data);
24:   void show();
25: };
26: 

Simple Queue in C using Fixed size array

1: #include <stdio.h>
2: 
3: //implementing a queue(fifo) structure
4: //using array with constant size
5: //not using dynamic memory
6: 
7: #define CAP 10
8: 
9: int enqueue(int *q,int *size,int data)
10: {
11:   if(*size == CAP)
12:     return 0;
13:     
14:   q[(*size)++] = data;
15:   return 1;
16: }
17: 
18: int dequeue(int *q, int *head)
19: {
20:   return q[(*head)++];
21: }
22: 
23: int main(int argc, char **argv)
24: {
25:   int arr[CAP];
26:   int head = 0;//starting index of the queue
27:   int size = 0;//size of the queue
28:   int i = 0;
29: 
30:   while(enqueue(arr,&size,i++));
31:   
32:   for(;;)
33:   {
34:     if (head == size)
35:       break;
36:     else  
37:       printf("element is %d \n",dequeue(arr,&head));
38:   }
39:   getchar();
40:   printf("hello world\n");
41:   return 0;
42: }
43: 

Sunday, June 6, 2010

Stack using list in C++

In case of c++ we have a different way of passing the head pointer to the push and pop functions
  1. If we are using the **head notation to pass a reference to the head pointer, then we have to explicitly use *head in the functions and &head while calling the functions.
  2. But if we are using the *&head to pass the c++ compiler will take care of the extra *and we don't have to put the extra * and & while using the head reference in the function or passing the reference argument respectively.
1: #include <stdio.h>
2: #include <iostream>
3: 
4: using namespace std;
5: 
6: //stack in c++ using link list
7: //using c++ style reference passing for head pointer
8: //ie passing *&head
9: 
10: #define INCREMENT 10
11: class stack
12: {
13:   typedef struct node
14:   {
15:     struct node *next;
16:     int data;
17:   }stackNode;
18:   
19:   int m_nCapacity;
20:   int m_nSize;
21:   stackNode *m_pHead;
22: public:
23:   stack(int capacity = INCREMENT);
24:   stack(const stack &theStack);
25:   stack& operator = (const stack &theStack);
26:   bool push(stackNode *&head,int data);
27:   int pop(stackNode *&head);
28:   stackNode*& getHead(){return m_pHead;}
29:   void show();
30: };
31: 

Stack Using Dynamic Array in C

1: #include <stdio.h>
2: #include <malloc.h>
3: 
4: #define INCREMENT 30
5: 
6: int capacity = 0;
7: int size = 0;//this will also be used as a index
8: //number of elements popped, 
9: //used in reducing the size of the array
10: int popCount = 0;
11: 

Friday, June 4, 2010

Stack using link list in C

1: #include <stdio.h>
2: #include <malloc.h>
3: #include <ctype.h>
4: 
5: //stack implementation using link list
6: typedef struct node
7: {
8:   int data;
9:   struct node *next;
11: }stackNode;
12: 
13: stackNode *head = NULL;
14: int size = 0;
15: