1: #include <stdio.h>2:3: typedef struct node4: {5: struct node *next;6: int data;7: }listNode;8:9: //passing the **head so that head can be modified10: int insert(listNode **head,listNode *node)11: {12: if(*head == NULL)13: *head = node;14: else15: {16: node->next = *head;17: *head = node;18: }19: return 1;20: }21:
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
Saturday, June 12, 2010
Link List In C++
1: #include <stdio.h>2: #include <iostream>3:4: using namespace std;5:6: class linkList7: {8: typedef struct node9: {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 list21: int count(int find);22: int getNth(int index);//index starting from 023: //removes all the nodes, frees and set head null24: void deleteList();25: //pop the head, changes the head26: int pop();27: //inser data at nth index28: 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 105: //in case of c++, if there is no need to pass the pointers6: //of head and tail as they are members of the class itself7: //and the member functions can access them and modify them8: class queue9: {10: typedef struct node11: {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 105: //if we are passing the pointers then they have to passed by6: //reference ie *&7:8: class queue9: {10: typedef struct node11: {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) structure4: //using array with constant size5: //not using dynamic memory6:7: #define CAP 108: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 queue27: int size = 0;//size of the queue28: int i = 0;29:30: while(enqueue(arr,&size,i++));31:32: for(;;)33: {34: if (head == size)35: break;36: else37: 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: #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 1011: class stack12: {13: typedef struct node14: {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 305:6: int capacity = 0;
7: int size = 0;//this will also be used as a index8: //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 node7: {8: int data;
9: struct node *next;
11: }stackNode;12:13: stackNode *head = NULL;14: int size = 0;
15:
Subscribe to:
Posts (Atom)