Professional Documents
Culture Documents
com
lists
Assist. Assoc. Dr. Aybars UĞUR,
Assist.Prof.Dr. Prepared from M. Ali
Akcayol lecture notes and foreign
resource compilations.
one
2nd
LISTS
- Lists
in daily life; shopping lists, invitations,
phone lists etc. used.
- Different
forms of lists are used in data
structures and different operations are
performed on them.
3
- The array is defined at the beginning of the program and the memory space to be allocated is
specified. The number of elements cannot be increased or decreased while the program is
running.
4
LINKED LISTS
- Lists that do not have consecutive elements in memorylinked
list is called.
LINKED LISTS
- Linked list structures are similar to two-dimensional
array structures. It also has the properties of a one-
dimensional array.
LINKED LISTS
- Thereis no dynamic approach to linear data structures.
When requested, memory space cannot be taken or
available memory space cannot be returned. Linked lists
are dynamic data structures and allow the above
operations to be performed. Memory sizes called nodes
are used in linked lists.
LINKED LISTS
- Comparing Linked Lists and Arrays:
- Series;
- Resize is difficult
- Adding a new element is difficult
- Deleting an element is difficult
Mahmut 0
Mehmet one
Angel 2nd
text Murat 3
Mustafa 4
5
9
LINKED LISTS
LINKED LISTS
- Example: Linked list structure in C language
struct ListNode
{ int data;
struct ListNode *next;
}
- Theaddress of the first element of the linked list global can be kept in a
pointer. If the pointer to the last element isNULL left as the address.
Linked list elementsmalloc It is created by dynamic memory functions
such as
LINKED LISTS
- lists;
- Each element in the list data (data) and link (link) part.
It refers to the information stored in the data part.
The link part points to the next element.
Structure of a node
12
LINKED LISTS
current
- Control
- empty list
- List size
14
- There
is only one link between the elements in the list. In
such linked lists, the direction of movement is only
from the beginning to the end of the list.
tail
head
15
8 8 one 2nd 3
Head A1 B 3 NS3 F0
x=5
D8
16
new=null
first = null
Workman
TO TO
my node new=new my node()
will be added first=null first = new
new.data
me?
H
H
Finish it new.later=first
first=new
17
x=9
G
8 one 2nd 3 9
Head A one B2 C4 F9 G 0
19
Start
new=null
first = null
end=null
Workman
TO TO new.queue=null
my node new=new my node()
will be added first=null first = new
new.data
me? last=new
H
H
new.queue=null
Finish it
last.queue=new
last=new
20
x=4
TO
8 one 2nd 3
E3
22
new=null
first = null
active=null
Workman
TO TO new.queue=null
my node new=new my node()
will be added first=null first = new
new.data
me? active=new
H
H
Finish it
TO
new.queue=active.queue
active.queue=new
first=active
23
8 one 2nd 3
one 2nd 3
Head A1 B 3 NS3 F0
.
24
Start
list=first
Workman
H
list.data
elixlteen=encuelkl list=list.queue
me?
TO
Finish it
25
Start
search=first
Workman
H H
chargecellk
with aa=nneu search.data=x search=search.queue
me?
TO TO
Finish it
26
delete=first
previous=first
erai=nx
sEill.evm
H
previous=delete
will add
delete=delete.queue
=ni?ull
delete
TO
TO
delete=null
Not found
TO
delete=first first=first.queue Finish it
H
previous.queue = delete.queue
27
tail
head
28
Start
new=null
first = null
end=null
Workman
TO TO new.then=null
my node new=new my node()
will be added first=null first = new
new.data
me? last=new
H
H
new.then=null
Finish it last.later=new
new.once=last
last=new
29
LB WORK
one 2nd 3 4 5
0A 2nd one
B 6 6 D 4 3 TO5 4 F0
2C3
30
WORK
LB
one 2nd 3 4 5
Start
list=first
Workman
H
list.data
elixlteen=encuelkl list=list.then
me?
TO
Finish it
32
delete=first
erai=nx
sEill.evm
H
will add delete=delete.then
=ni?ull
delete
TO
TO
delete=null
Not found
TO first.after.once=null
delete=first first=first.later Finish it
H
delete.once.later=delete.then
delete.then.once=delete.once
33
- Bidirectional Circular Linked Lists : They are lists with both circularity
and double dependency properties. The node before the first node is
the last, and the node after the last node is the first.
active
tail
34
- class Node
-{
- public int data; // can be reproduced in different types
- }
37
- class BListTest {
- public static void main(String args[]) {
- BList list = new BList(); // creates a linked list object named list. Add
- to list.base(9);
- for(int i=8; i>=1; --i) add to list.basa(i); //add to list.end(i);
- list.list(); int value = 5; Node d = list.find(value);
- if(d==null) System.out.println("\n"+value+" Not in List");
- else System.out.println("\n"+value+" Found"); node
- s = list.delete(5);
- list.list(); Screen Output:
- } // End-to-end List: 1 2 3 4 5 6 7 8 9 // 5
- } Found
// End-to-end List: 1 2 3 4 6 7 8 9
41
- erasing over
- Delete in desired order
- end deletion
- structures will be created by you on this example and
delivered to the lecturer.
42
Homework
- 2nd-
- Show the number and letter averages of N students for K courses with
connected sequences.
- Each node will contain the student number, the code of the course, and the
letter average of the course.
- Each student's number will be the information in the headNode. The code
- Each node will display the next student in the same course. Each
- node will show the other course of the same person.
- The program will be prepared as Java or C# Windows application and will do the following
operations with the buttons.
- 1- Adding a new lesson to a student 2-
- Adding a new student to a lesson 3-
- Deleting a lesson of a student 4-
- Deleting a student from a lesson
- 5- Listing all the students in a course in order of numbers 6- Listing all the
- courses a student has taken in order according to the course code
43
Homework
- erasing over
- Delete in desired order
- end deletion
Valiant
(stack)
44
45
Stack/Stack
- Lastin, first out (Last In First Out - LIFO) or First in, last out(
First-in-Last-out FILO) works with logic.
Stack/Stack
- There are 2 ways to perform the stack structure.
- Using an array
- Using a linked list
Stack Operations
- Main stack operations:
- push(object): adds a new object
- Entry: Article Output: No
- pop(): Removes and returns the most recently added object.
- Entry: No Output: Article
Stack Structure
push(3);
push(2); push(11);
push(12); 12 push(5);
pop();
12 2nd 5
2nd 3 11th
3 2nd
3
Stack/Stack
- Example uses
- Undo operations in software applications are done with
stack. LIFO structure is used for Undo operation.
- TheBack button (to previous page) in web browsers uses
stack. Here, the LIFO structure is used.
- Stack
can be used for operators (such as +,*,/,-) and
operands in mathematical operations.
- Stack can be used to check parentheses in spell
check.
50
Stack/Stack
- Example: Adding and removing from the stack
Array-Based Stack
- Theeasiest way to implement a stack is to use
an array. The stack structure can be made to
hold a maximum of N elements on the array.
12
12 2nd 11th
2nd 3 2nd
3 3
pop(); push(11);
52
Array-Based Stack
- We add objects from left to
right. A variable follows the
index of the topmost
object. Workman
This index value is taken when
extracting.
53
Array-Based Stack
- The array in which the stack objects are stored
- FullStackException
(FullStackException)
- This is the limitation of the array-
based approach.
54
if (fill()){
// The stack is full. New elements cannot be
added.return -1;
}
S[p] = new;
p++;
return 0;
} /done-put
59
return S[p-1];
}
Translated from Turkish to English - www.onlinedoctranslator.com
60
return S[id];
}
61
if (y.bosmu())
System.out.println("The stack is empty");
y.put(49); y.koy(23);
y.koy(44); y.koy(22);
put(3)
Top
3 9 2nd 6 15 After adding 3
get()
Top
9 2nd 6 15 3 after receiving
get()
Top
2nd 6 15 after 9 received
63
My Worship(int) to){
element = e; next = NULL;
}
}
return master.element;
} //done-top
66
return temp.element;
} //do-get
67
if (y.bosmu())
System.out.println("The stack is empty");
y.put(49); y.koy(23);
Examples –Java
- There is also a ready-made Stack class in Java. In the
example below, the StringsNS placed on the stack and
listed in reverse order.
- import java.util.*;
- {
- public static void main(String args[])
- s.push(str[i]);
while(!s.empty() ) System.out.println(s.pop());
- }
- }
69
Practice Assignment
- Compilers/word processors
- If we think about compilers, they check whether the
parentheses in the expression we write are the same.
- For example: The expression 2*(i + 5*(7 – j / (4 * k)) lacks
parentheses. ")"
- Write a program that checks if the expression has an
even number of parentheses using the stack.
70
Practice Assignment
EXAMPLES
72
Examples- C++
- Example 1: One-way linked list
- #include <stdio.h>
- #include <stdlib.h>
- main() {
- struct notes {
- int visa1;
- struct notes *bag;
-} *newaddress, *first, *last;
- newaddress=(struct notes*) malloc(sizeof(struct notes));
- printf("Newaddress:%d\n",newaddress);
- first=newaddress;
- end=newaddress;
- newaddress->visa1=79;
- newaddress->bag=NULL;
73
C++
- printf("first:%d\n",first); //Holds the address of the first element
- printf("T1___newaddress->visa1:%d newaddress->bag:%d\n",newaddress->visa1,
newaddress->bag);
- //Request place from memory
- newaddress->visa1=95;
- newaddress->bag=NULL;
- end=newaddress;
- printf("first:%d\n",first);
- printf("end:%d\n",final);
- printf("T2___newaddress->visa1:%d newaddress->bag:%d\n", newaddress->visa1,
newaddress->bag);}
74
LINKED LISTS
- Requesting a new address from memory on first operation
new
new
end
75
LINKED LISTS
- Requesting a new address from memory in the second operation
new
LINKED LISTS
new new
7332
7344 0
77
C++
78
Examples - C++
- Example: 2- Storing movie names with bidirectional linked list
- # include <stdio.h>
- # include <stdlib.h>
- # include <string.h>
- # define TSIZE 45
- struct film {
- char title[TSIZE];
- int rating;
- struct film * next, * previous; } ;
- int main(void) {
- struct filim * first = NULL;
- struct film * current, *last;
- char input[TSIZE];
- puts("Enter the title of the movie (enter (space) to end):");
79
C++
- while (gets(input) != NULL && input[0] != '\0') {
- now = (struct film *) malloc(sizeof(struct film)); if
- (first == NULL) first = current;
- else {last->next = current; current->previous=last;}
-
- current->next = NULL;
- strcpy(current->title, input);
- puts("Enter rating <0-10>:");
- scanf("%d", ¤t->rating);
- while(getchar() != '\n') continue;
- puts("Enter the title of the movie (enter a space to end):"); last
- = current;
- }
80
C++
- if (first == NULL)
- printf("Data is not entered.");
- else
- printf("Movie List:\n");
- current = first;
- while (current != NULL) {
-
- printf("Movie: %s Rating: %d\n", current->title, current->rating);
- current = current->next;
- }
- current = first;
- }
81
present
present present
- if (rBBas.Checked)
- connectedListe.headNode.next = connectedList.headNode.next.next;
- else if (rBKisi.Checked)
-{
- ListNode active = connectedListe.headNode;
- while((active.next!=connectedList.tailNode) &&(active.next.number!=tBNum.Text))
- active = active.next;
- active.next = active.next.next;
-}
89
- Adding an element
- ListNode newNode = new ListNode(“Mustafa");
- newNode.next = active.next;
- active.next.previous = newNode;
- active.next = newNode;
- newNode.previous = active;
92
- Delete an element
- while ((active.next != connectedList.tailNode) && (active.next != to be deletedNode)) {
-
- active = active.next;
- }
- active.next.next.previous = active;
- active.next = active.next.next;
93
- Element search
- while ((active.next != connectedList.tailNode) && (active.next != searchedNode))
- { active = active.next;}
- while ((active.previous != connectedList.headNode) && (active.previous != searchedNode)) { active
- = active.previous; }
94
- class Node
- {
- public object data; // data to be kept in the node
- public Node previous, next; // Node's address information
- public Node(object data)
- {
- this.data = data;
- this.previous = null;
- this.next = null;
- }
- }
- public class BList
- {
- Node firstNode = new Node(“”);
- Node sonNode = new Node(“”);
- public int s = 0;
- public BList() // Linked list structure is set {
-
- this.firstNode.next = this.sonNode;
- this.sonNode.previous = this.firstNode;
95
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1_4.WEEK {
-
- class Node
- {
- public object data; // data to be kept in the node
- public Node previous, next; // Node's address information
- public Node(object data)
- {
- this.data = data;
- this.previous = null;
- this.next = null;
- }
- }
- public class BList
- {
96
- class Node {
-
- object data;
- Node prev;
- node next;
-
- public Node( Object data ) {
- this.data = data;
- }
-
- public Node( Object data, Node prev, Node next ) {
- this.data = data;
- this.prev = prev;
- this.next = next;
- }
- }
-
- public class CircularDoublyLinkedList {
-
97
Examples - C++
- Example:- Bidirectional linked list
-
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# include <string.h>
struct person *new , *first , *last;/* We made new, first and last name definitions in the struct person type.*/
/*Gulnur CAKIR*/
98
Examples - C++
- int main()
{
new=NULL; first=NULL; end=NULL; /* We made all structs NULL */
{
case one : myname_add(); break;
case 2nd : list(); break;case 3 :
delete(); break;case 4 : exit(one);
break;
}
99
Examples - C++
- /* Add Node Function */void
word_add()
{
if(first==NULL)/* Is there an employee in my home?*/{
/* If there is no element in my case, first=NULL.*/
struct contact *new = ((person *) malloc(sizeof(person))); /*We have just defined the struct person type and the size of the struct from memory
how far we got */
printf("\n\tPlease enter the name of the person : ");scanf("%NS",&new-
>name);/*New information received from user*/new->then=NULL;
/* NULL since there is no element after newly received information */
first=new; last=new; /* The new and last policy is synchronized since it is the first element retrieved for the node*/
}
else
{
struct contact *new = ((person *) malloc(sizeof(person)));
- /* We just defined the struct as the person type and took up memory space as much as the size of the
struct */printf("\n\tPlease enter the name of the person : ");scanf("%NS",&new->name);/*New
information received from user*/new->then=NULL;
/* NULL because there is no element after the newly received information */ /*
last->after=new; The pointer of a previous struct named after is set to the new one*/
new->once=last; / *The only difference is from creating a one-way linked list*/
/* The pointer of the currently created struct named once is synchronized to the previous struct*/
last=new;
/* The struct named last is now its new struct. The purpose of doing this when a new node will be added
knowing which struct was added last
Equalizing the last added struct to the new struct, making it equal the pointer named once of the newly created struct
to the end.*/
}
printf("\n\tPress any key to return to the main menu...");
getch();
menu();
}
one hundred
Examples - C++
- /*Record listing*/void list()/* Same as listing a one-way
linked list*/{
printf("\t\t->%s\n",search->name);
}
printf("\n\tPress any key to return to the main menu...");
getch();
menu();
}
/***********************************/
101
Examples - C++
- /*Delete record*/
void deletion()
{ char name[20]; struct contact *delete;
}
if(clear==NULL)
printf("\tNot Found.\n");else
if(delete==first)
{ first->after->once=NULL;free first=first->then;
(delete); printf("\tRecord deleted.");
}
else if(delete==end)
{ last=delete->before; delete->once->then=NULL;
free(delete); printf("\tRecord deleted.");
}
else
{ delete->once->then=delete->next;
delete->then->once=delete->once;
free(delete); printf("\tRecord deleted.");
}
printf("\n\tPress any key to return to the main
menu...");getch(); menu();
}
102
- # include "stdio.h"
- # include "conio.h"
- # include "stdlib.h"
- # include "string.h"
- # include "iostream.h"
- //NAME OF THE PLAYERS---------------------------------------------- -------------------
- char *names[10]= {"HACER","GULTEN","IDRIS", "HASAN","HATICE",
"CEMIL","BELMA", "CANSU","EBRU","NALAN"};
- //------------------------------------------------ -------------------------------------------
- typedef struct data{
- char name[20];
- struct data *back;
- } BLISTE;
- BLISTE
-
- if(first==NULL)
- {
- first=number arrived;
- last=first;
- first->back=first;
- }
- else
- {
- last->back=numbers arrived;
- end=numbers arrived;
- last->back=first;
- }
- return 0;
- }
- //------------------------------------------------ -----------------------------
104
- y+=13;
- gotoxy(x,y);printf("TOTAL NUMBER OF PLAYERS: %d ",i); getch();
- return 0;}
- //------------------------------------------------ ----------------------------
105
- void main(void) {
- clrscr(); randomize();
- BLISTE *new;int i,x=3,y=3;
- for( i=0;i<10;i++) {
- new=(BLISTE *)malloc(sizeof(BLISTE));
- if(!new) { clrscr();
- printf("Sorry, there is not enough free space to add players"); exit(0); }
-
- else { strcpy(new->name,names[i]); add(new); }
- }
- clrscr();x=3;y=3; int
- originating; players();
- new=first; BLISTE *p; BLISTE *previous;
107
Examples -Java
- Example: Creating and using a string class using arrays in
Java.
- import java.io.*;
- class StackChar
- {
- private int maxSize; private char[] stackArray; private int top;
- public StackChar(int max)
- { maxSize = max; stackArray = new char[maxSize]; top= -1; }
Examples –Java
- class Reverse
-{
- public static void main(String args[]) {
-
- StackChar y = new StackChar(100);
- String str = "Hello";
- for(int i=0; i<str.length(); ++i)
- y.push(str.charAt(i));
- while(! y.isEmpty() ) System.out.println(y.pop());
- }
-}
110
Examples –C#
- Sample: Stacking with linked list
- class stackNodeC
-{
- public string bookName; public stackNodeC next;
- public stackNodeC(string bookName) { this.bookName = bookName; }
-}
- class stackC
-{
- public stackNodeC headNode;
- public stackC(string bookName)
-{
- this.headNode = new stackNodeC(bookName);
- this.headNode.next = headNode;
-}
-}
- stackC bookYigin = new stackC("");
-
111
Examples –C#
- /* Stack operations:
- empty stack stackSize() == 0 number of elements = stackSize()
- add element = push(bookName) get elements = pop() */
Examples –C#
- // Stack operations (add elements)
- public void push(string bookName) {
-
- if (stackSize() >= MaxSize)
- MessageBox.Show("The heap has the maximum number of elements !
- else
- { stackNodeC newNode = new stackNodeC(tBBookName.Text);
- newNode.next = bookYigin.headNode;
- bookYigin.headNode = newNode;
- lStackSize.Text = "Stack Size =
- "+Convert.ToString(stackSize());
- }
- }
113
Examples –C#
Examples –C++
Examples –C++
- if (peak != NULL) {
- spare = peak;
- strcpy(outgoing, vertex->word);
- peak = peak->previous; free(reserve); return 0; }
- else
- return 1; /* stack is empty */
116
Examples –C++
- main() {
- char *rename, *sentence;
- peak = NULL;
-
- printf("Enter sentence: "); gets(sentence);
- newword = strtok(sentence, " "); // do not separate the sentence variable by
space.
-
- while (refresh) {
- push (refresh);
- newword = strtok(NULL, " ");
- }
- newword = (char *) malloc(20); /* newword was NULL, let's make room*/
- while (!pop(renewword)) printf("%s ", newword);
- printf("\n");
- getch();
- }
117
Examples –C++
118
Examples –C++