You are on page 1of 22

HY-240

LSTS
AHAA ZYNAEAEMENH AZTA
YAOHOHZH ZE C
AHAA ZYNAEAEMENH AZTA
AHAA ZYNAEAEMENH AZTA
:OPZMOZ AOMHZ FA KAOE KOMBO
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char *word;
int num;
struct node *next;
} *head;
AHAA ZYNAEAEMENH AZTA
:AHMOYPFA - APXKOHOHZH KA
ZYNAEZH KOMBOY
void new_node (char *word, int number)
{
struct node *p, *here;
p=(struct node *)malloc(sizeof(struct node));
p->word= strdup(word);
p->num= number;
p->next= NULL;
/*ean den einai o prwtos kombos ths listas*/
if(head!=NULL)
{
here = head;
//kanw to head na deixnei ston teleutaio kombo ths listas
while(here->next!=NULL)
here=here->next;
here->next= p;
}
else //kataskeuazw ton prwto kombo ths listas auths
head=p;
return;
}
AHAA ZYNAEAEMENH AZTA:
ANAZHTHZH KOMBOY ZTHN AZTA
int find_node (char *word)
{
struct node *here;
here = head;
/*Ksekinaw apo thn arxh ths listas k psaxnw mexri to telos ths
se poio kombo brisketai h leksh. An thn brw auksanw to
metrhth tou kombou autou kata 1 */
while (here!=NULL)
{
if (!strcmp(here->word, word))
{
(here->num) ++;
return(1);
}
here= here->next;
}
return (0);
}
AHAA ZYNAEAEMENH AZTA:
AAFPA4H KOMBOY AHO THN AZTA
void remove_node(char *word)
{
struct node *here= head, *prevhere=NULL;
/*proxwraei kombo-kombo mexri na brei auton pou periexei th leksh pou thelw*/
while(here!=NULL && strcmp(word, here->word))
{
prevhere=here;
here= here->next;
}
/* an exei brei kombo me thn sygekrimenh leksh mpainei sthn if alliws termatizei*/
if(here!=NULL)
{
/*ean o kombos here den einai to head*/
if(prevhere!=NULL)
prevhere->next= here->next;
/* an diagrafw to head*/
else
head= here->next;
free (here->word);
free (here);
}
return;
}
AHAA ZYNAEAEMENH AZTA:
AAFPA4H AZTAZ
void delete_list()
{
struct node *tmp= NULL;
/*proxwraei kombo-kombo*/
while(head!=NULL)
{
tmp = head;
head= head->next;
free (tmp->word);
free (tmp);
}
}
AHAA ZYNAEAEMENH AZTA:
EKTYHCZH AZTAZ
void print_list()
{
struct node *here= head;
/*proxwraei kombo-kombo kai tous ektupwnei olous*/
while(here!=NULL)
{
printf("word= %s ", here->word);
printf("number= %d \n", here->num);
here= here->next;
}
printf("\n");
}
AHAA ZYNAEAEMENH AZTA:
AOKMH AETOYPFCN AZTAZ
int main()
{
new_node("Xrysh", 2);
new_node("Manolhs", 4);
new_node("Maria", 1);
new_node("Nikos", 13);
print_list();
remove_node("Maria");
print_list();
delete_list();
return(0);
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA: TAENOMHZH KATA THN
EZAFCFH
Sort( Node *Head)
{
node* first, second, temp;
first= Head;
while(first!=null)
{
second=first->next;
while(second!=null)
{
if(strcmp(first->word, second->word) < 0)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->word=first->word;
first->word=second->word;
second->word=temp->word;
temp->num=first->num;
first->num=second->num;
second->num=temp->num;
free(temp);
}
second=second->NEXT;
}
first=first->NEXT;
}
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA: EZAFCFH ZTHN KATAAAHAH
OEZH
void insert(char *data, int number) {
struct node *p;
struct node *q;
p = (struct node *)malloc(sizeof(struct node));
p->word = strdup(data);
p->num = number;
if(list == NULL || strcmp(head->name, data) > 0)
{
p->next = head;
head = p;
}
else
{
q = head;
while(q->next != NULL && strcmp(q->next->word, data) < 0)
{
q = q->next;
}
p->next = q->next;
q->next = p;
}
}
AHAA ZYNAEAEMENH AZTA
YAOHOHZH ZE JAVA
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):OPZMOZ KOMBOY
public class LinkedList
{
private class Node
{
Object data;
public Node(Object _data)
{
next = null;
data = _data;
}
public Node(Object _data, Node _next)
{
next = _next;
data = _data;
}
public Object getData() { return data; }
public void setData(Object _data) { data = _data; }
public Node getNext() { return next; }
public void setNext(Node _next) { next = _next; }
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):APXKOHOHZH KA
EZAFCFH
private Node head;
private int listCount;
public LinkedList()
{
head = new Node(null);
listCount = 0;
}
public void add(Object data)
{
Node temp = new Node(data);
Node current = head;
while(current.getNext() != null)
{
current = current.getNext();
}
current.setNext(temp);
listCount++;
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):EZAFCFH ZE
ZYFKEKPMENH OEZH
public void add(Object data, int index)
{
Node temp = new Node(data);
Node current = head;
for(int i = 1; i < index && current.getNext() != null; i++)
{
current = current.getNext();
}
temp.setNext(current.getNext());
current.setNext(temp);
listCount++;
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):EHZTPO4H AEAOMENCN
ZTHN OEZH
public Object get(int index)
{
if(index <= 0)
return null;
Node current = head.getNext();
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return null;
current = current.getNext();
}
return current.getData();
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):AAFPA4H THZ OEZHZ
public boolean remove(int index)
{
if(index < 1 || index > size())
return false;
Node current = head;
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return false;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
listCount--;
}
AHAA ZYNAEAEMENH TAENOMHMENH
AZTA(java):EHZTPO4H MEFEOOYZ KA
EKTYHCZH
public int size()
{
return listCount;
}
public String toString()
{
Node current = head.getNext();
String output = "";
while(current != null)
{
output += "[" + current.getData().toString() + "]";
current = current.getNext();
}
return output;
}
!roject: First Deliverable
Hspoqvid Hdpooq 29/10
E/ioo d0 project: 4
Hpsi vd dpdo0ov /s oi osi
!roject: First Deliverable

You might also like