Вы находитесь на странице: 1из 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "header.h"
void addnew (struct Node** head_ref, struct Node* p)
{
struct Node* q = (struct Node*) malloc(sizeof(struct Node));
int new_data;
scanf("%d",&new_data);
q->data=new_data;
if(p == NULL)
{
q->next = *head_ref;
*head_ref = q;
return;
}
else
{
q->next = p->next;
p->next = q;
}
}

void addNode(struct Node **head_ref, int new_data)


{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}

void delNode(struct Node **head,struct Node *n)


{
if(*head == n)
{
if((*head)->next == NULL)
{
printf("There is only one node. The list can't be made
empty ");
return;
}
(*head)->data = (*head)->next->data;
n = (*head)->next;
(*head)->next = (*head)->next->next;
free(n);
return;
}
struct Node *prev = *head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
if(prev->next == NULL)
{
printf("Given node %p is not present in Linked List\n",n);
return;
}
prev->next = prev->next->next;
free(n);
}
void deleteNode(struct Node **head_ref, int position)
{
if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if (position == 1)
{
*head_ref = temp->next;
free(temp);
return;
}
for (int i=1; temp!=NULL && i<position-1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
struct Node *next = temp->next->next;
free(temp->next);
temp->next = next;
}
int listLength(struct Node* pPrim, unsigned int len)
{
len = 0;
struct Node* temp = pPrim;
while(temp != NULL)
{
temp = temp->next;
len++;
}
return len;
}

void printList(struct Node* node)


{
while (node != NULL)
{
printf("%d ",node->data);
node = node->next;
}
puts("");
}
/*int* MAX(struct Node* head,int max,struct Node* pmax)
{
max = -2147483648 ;
while (head != NULL)
{
if (max < head->data)
{
pmax = head;
max = head->data; //PENTRU POINTER
}
head = head->next;
}
printf("Maximul si indexul lui: %d %x\n",max,pmax);
return pmax;
}

int* MIN(struct Node* head,int min,struct Node* pmin)


{
min = 2147483647 ;
while (head != NULL)
{
if (min > head->data)
{
pmin = head; //PENTRU POINTER
min = head->data;
}
head = head->next;
}
printf("Minimul si indexul lui: %d %x\n",min,pmin);
return pmin;
}*/

BOOL isEmpty(struct Node* prim)


{
return ( prim == NULL ) ? True : False;
}

int MAX(struct Node* head)


{

int max = -2147483648 ;


while (head != NULL)
{
if (max < head->data)
max = head->data;
head = head->next;
}
printf("Maximul: %d\n",max);
return max;
}

int MIN(struct Node* head)


{
int min = 2147483647 ;
while (head != NULL)
{
if (min > head->data)
min = head->data;
head = head->next;
}
printf("Minimul: %d \n",min);
return min;
}

int searchList(struct Node* prim, int nr)


{
int i=1,index;
struct Node* temp = prim;
while(temp != NULL)
{
if(nr == temp->data)
index = i;
temp = temp->next;
i++;
}
return index;
}

Вам также может понравиться