Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct Node {
- int data;
- Node *next;
- } Node;
- Node *head = NULL;
- Node* creatNode(int val){
- Node* temp;
- temp = new Node;
- //temp = malloc(sizeof(Node));
- temp->data = val;
- temp->next = NULL;
- // cout << temp->data << endl;
- return temp;
- }
- Node* AddHead(int val){
- Node* new_node = creatNode(val);
- if (head == NULL){
- head = new_node;
- } else {
- new_node->next = head;
- head = new_node;
- }
- }
- void print_llist(){
- Node* p = head;
- while(p != NULL){
- cout << p->data << " ";
- p = p->next;
- }
- }
- int findNode(int val){
- Node *currentNode = head;
- int currentIndex = 1;
- while (currentNode && currentNode -> data != val) {
- currentNode = currentNode -> next;
- currentIndex++;
- }
- if (currentNode) {
- return currentIndex;
- } else return 0;
- }
- int main(){
- for(int i = 0; i < 10; i++){
- AddHead(i*i);
- }
- print_llist();
- cout << endl << findNode(64);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment