Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Kogato tursim daden element(key), toi stava pyrvi vuv spisyka, taka sledvashtiq pyt vremeto za dostyp e constanta.
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- }* first = NULL;
- void create(int A[], int n)
- {
- int i;
- struct Node *t, *last;
- first = (struct Node*)malloc(sizeof(struct Node));
- first->data = A[0];
- first->next = NULL;
- last = first;
- for (i = 1; i < n; i++) {
- t = (struct Node*)malloc(sizeof(struct Node));
- t->data = A[i];
- t->next = NULL;
- last->next = t;
- last = t;
- }
- }
- void Display(struct Node* p)
- {
- while (p != NULL) {
- printf("%d ", p->data);
- p = p->next;
- }
- }
- struct Node* LSearch(struct Node* p, int key)
- {
- struct Node* q = NULL;
- while(p!=NULL)
- {
- if(p->data == key)
- {
- q->next = p->next;
- p->next = first;
- first = p;
- return p;
- }
- else
- {
- q = p;
- p = p->next;
- }
- }
- return NULL;
- }
- struct Node* RSearch(struct Node* p, int key)
- {
- if (p == NULL)
- return NULL;
- if (key == p->data)
- return p;
- return RSearch(p->next, key);
- }
- int main()
- {
- struct Node* temp;
- int A[] = { 3, 5, 7, 10, 25, 8, 32, 2 };
- create(A, 8);
- Display(first);
- printf("\n ");
- temp = LSearch(first, 8);
- printf("% d", temp->data);
- printf("\n ");
- Display(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement