Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- }* first = NULL;
- int count = 0;
- 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;
- }
- }
- void LinkedListDisplay(struct Node* p)
- {
- if(p!=NULL)
- {
- printf("%d", p->data);
- LinkedListDisplay(p->next);
- }
- }
- void LinkedListDisplayReverse(struct Node* p)
- {
- if(p!=NULL)
- {
- LinkedListDisplayReverse(p->next);
- printf("%d ", p->data);
- }
- }
- void LinkedListCount(struct Node* p)
- {
- if(p!=NULL)
- {
- count++;
- LinkedListCount(p->next);
- }
- else
- printf("The number of elements in the linked list is: %d ", count);
- }
- void LinkedListSum(struct Node *p)
- {
- int sum = 0;
- while(p)
- {
- sum +=p->data;
- p = p->next;
- }
- printf("\n The of the linked list is: %d", sum);
- }
- /* Ne raboti
- void LinkedListImprovedSearch(Node *p, int key)
- {
- Node* q = NULL;
- while(p)
- {
- if(p->data == key)
- {
- q->next = p->next;
- p->next = first;
- first = p;
- }
- else
- {
- q = p;
- p = p->next;
- }
- }
- }
- */
- int main()
- {
- struct Node* temp;
- int A[] = { 3, 5, 7, 10, 25, 8, 32, 2 };
- create(A, 8);
- Display(first);
- LinkedListDisplayReverse(first);
- printf("\n");
- LinkedListCount(first);
- LinkedListSum(first);
- printf("\n ");
- Display(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment