What Is Data Structure?: Must Be NULL
What Is Data Structure?: Must Be NULL
When we want to work with unknown number of data values, we use a linked list data
structure to organize that data. Linked list is a linear data structures that contains
sequence of elements such that each element links to its next element in the
sequence. Each element in a linked list is called as Node.
Every Node contains two fields, data and next. The data field is used to store actual
value of the node and next field used to store the address of the next node in the
sequence.
NB in a single linked list, the address of the first node is always stored
in a reference node known as front {sometimes known as head}
Always next part (reference part) of the last node must be NULL
Data
Link
Stores actual
value
Example of Linear List
Mode
Address
1001
10 1004
1004
25 1008
1008
18 1012
*Font
1001
Operations
In a single linked list we perform the following:
-
Insertion
Deletion
Display
1012
55 NULL
Before we implement actual operations first we need to set up an empty list. First perform the
following steps before implementing actual operations.
-
Step 1: Include all the header files which are used in the program
Step 2: Declare all user defined functions
Step 3: Define a node structure with two members, data and next
Step 4: Define a node pointer head and set it to NULL
Step 5: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user select operation.
Example:
Head
NULL
20
30
30
Current Node
NewNode
40
30
Step 1 create a newNode with given value and newNode next as Null
Step 2 check whether list is empty (head null)
Step 3 if it is empty then set head = newnode
Step 4 if it is not empty then, define a node pointer temp and initialize with head
Step 5 Keep moving the temp to its next node until it reaches to the last node in the list
(until temp next is equal to Null )
Step 6 Set temp next = newNode.
Example:
Newcode
30 Next
NULL
Temp
10 Next
20 Next
Head
Example:
head
temp
location
V
New name
Definition of a node
In a single linked list, the deletion operation can be performed in three ways. They are as
follows:
Deleting from the beginning of the list First create a temporary node pointer
that points to the head of a list. We then change the linked lists head pointer.
Finally, we delete the front of the list by deleting the node that the temporary
pointer points to (its points to the front node)
NULL
Diagram Explain:
In 1 we create our temporary pointer. IN 2 we move our head pointer one node
(3), we delete the node that the temporary pointer points to.
Deleting from the end of the list - The solution is to start at the front of the list
and move to the node that is before the last node
Diagram Explain:
Now our temporary node pointer points to the node that will become the tail of
our list. This nodes Next pointer also just so happens to point to the current tail.
All we have to do now is delete the node that our current nodes (the one that our
temporary pointer points to) Next pointer points to (which is the current tail), set
the Next pointer to NULL, and make our linked lists tail pointer point to the node
our temporary pointer points to. Heres the picture:
Diagram Explain
In (1), we have a pointer to the node that will become the new tail. In (2), we
delete the current tail. In (3), we change the linked lists tail pointer to point to the
new tail.
Deleting a specific node Removing a node from the middle of a list is a lot like
removing a node from the end. We create a temporary node pointer and move it to
the node before the one we want to delete. Now we have to be careful though.
Lets see what happens when we just delete the node like we did with the tail
node.
Here weve delete the third node in our list, in doing so, weve lost all reference to
the fourth node in the list. What we need is another temporary node pointer to
point to the 4th node while we delete the 3rd node. That way, we can reconnect our
list.
Diagram Explain
In (1), we have two temporary variables pointing to the nodes before and after the
node well be deleting it. In (2), we delete the node, in (3), we set the node before
the deleted node to the node after the deleted node. After this process, well have
a properly connected list, minus the deleted node.
Stack is a linear data structure in which the insertion and deletion operations are
performed at only one end. In a stack, adding and removing of elements are
performed at a single position which is known as top. That means from the top
pf the stack. In the stack, the insertion and deletion operations are performed
based on LIFO (Last In First Out).
Top
Tom
Pam
Ben
In a stack, the insertion operation is performed using a function called push and
deletion operation is performed using a function called pop.
In the figure, PUSH and POP operations at top positions in the stack. That means,
both the insertion and the deletion operations are performed at one end (i.e. at top)
If we want to create a stack by inserting 10, 45, 12, 16, 35 and 50. Then 10
becomes the bottom most element and 50 becomes the top most element. Top is
50 as shown in the image below:
50
Push 50 ( Because
there is still space in
the stack 50 will be
inserted on top of 35
and will become the
new Top)
Top
35
16
12
45
10
50
Top
35
16
12
45
10
Operations on a stack
The following operations are performed on the stack
Push (To insert an element on the stack)
Pop (to delete an element from the stack)
Display (to display elements of the stacks)
Stack data structure can be implemented in two ways. They are as follows
Using Array
Using Linked List
When stack is implemented using array, that stack can organize only limited number of elements.
When stack is implemented using linked list, that stack can organize unlimited number of
elements.
Stack Using an Array
Implementing a stack using arrays is very simple, just define a one-dimensional array of specific
size and insert or delete the values into that array by using LIFO principle with the help of a
variable top. Initially top set is set to -1.
Whenever we want to insert a value into the stack, increment the top value by 1 and then insert.
Whenever we want to delete a value from the stack, then delete the top value and decrement the
top value by 1.
Stack Operations using stacks
A stack can be implemented using as follows:
Before implementing actual operation, first follow the steps below to create an empty stack.
-
Step 1: Include all the header files which are used in the program and define a constant
SIZE with specific value.
Step 2: Declare all the functions used in stack implementation
Step 3: Create a one-dimensional array with fixed size (int stack[SIZE])
Step 4: Define a integer variable top and initialize with -1.(int top = -1)
Step 5: In main method display menu with list of operations and make suitable function
calls to perform operation selected by the user on the stack.
Step 3: If it is NOT EMPTY then define a variable i and initialize with top. Display stack[i]
value and decrement I value by one (i--).
Step 4 : Repeat the above steps until i reaches a value 0.
Examples.
1. The diagram below shows items stored in a stack. Which diagram gives the state of the
stack after the operations listed are performed in order:
Operations
Pop
Apple
Top
Melon
Pop
Push (Pear)
Mango
Pop
ANSWER
Mango
Top
2. The diagram below shows three items stored in a stack. Which sequence of operations
would transform the stack from the initial state to the final state shown below.
Initial Stock
Final State
Top
Top
ANSWER:
Pop, Pop, Push (C), Push (A)
Pop: Delete current Top C, new Top A; Pop: Delete current Top A, new Top is B; Push (C):
Enter C into the empty slot above B, C is the new Top; Push (A): Enter A into empty slot
above C, A is now the new Top.
3. The diagram below shows the state of a stack after a series of operations were performed.
What operations were used to reflect the stack shown below:
Adele
Top
Mary
John
ANSWER:
Push (John), Push (Mary), Pop, Push (Mary), Push (Adele)
Push (John): Enter John into empty slot, John is the new Top; Push (Mary): Enter Mary
into the empty slot above John, Mary is now the new Top; Pop: Delete current Top, Mary,
new Top is John; Push (Mary): Enter Mary into empty slot above John, Mary is the new
Top; Push (Adele): Enter Adele into empty slot above Mary, Adele is now the new Top.
int stack[MAXSIZE]
int top = -1;
int main()
{
Void push(int);
int pop();
int choice =1, i, num;
void clrscr();
While(choice == 1)
{
printf(MAINMENU: \n 1. Add element to stack \n 2. Delete element from
stack\n);
scanf(%d, &choice);
Switch(choice)
{
case 1:
printf(Enter the data);
scanf(%d, &num);
push(num);
break;
case 2: i = pop();
printf(Value returned from pop function is %d, i);
break;
default: printf(Invalid Choice);
}
printf(Do you want to do more operation (1 for yes, any other key to end.);
scanf(%d, &choice);
}//end of outer loop
}//end of main
Void push(int y)
{
If(top > MAXSIZE)
{
printf(STACK FULL);
return;
}
Else
{
top++;
stack[top] = y;
}
}
int pop()
{
Int a;
If (top <= -1)
{
printf(STACK EMPTY);
return 0;
}
Else
{
a = stack[top];
top--;
}
return (a);
}
Code 2
#include<stdio.h>
#define MAXSIZE 100
int stack[MAXSIZE];
int top = 0;
void push(int);
int pop (void);
void display (void);
int main()
{
int choice = 0; val = 0;
do
{
printf("\n \t 1. Push\n");
printf("\t 2. Pop \n");
printf("\t 3. Dislay \n");
printf("\t 4. Exit \n");
printf("\n \t Select your choice:");
scanf("%d", &choice);
Switch (choice)
{
case 1:
printf("\t Element to be Pushed:");
scanf("%d", &val);
push(val);
break;
case 2:
val = pop();
if (val= -1)
printf("\t Popped element: %d\n",val);
break;
case 3:
display();
break;
case 4:
break;
default("\t Wrong Choice");
break;
}
} while (choice != 4);
return 0;
}
void push(int val)
{
if (top < MAXSIZE)
stack[top++] = val;
else
printf("Stack overflow");
}
int pop()
{
int a;
if (top > 0)
{
top--;
a = stack[top];
return a;
}
else
{
printf("Stack is empty");
return -1;
}
}
void display()
{
int i = 0;
if (top > 0)
{
printf("\t Elements are:");
while (i < top)
{
printf("\t %d", stack[i++]);
}
print("\n");
}
else
printf("stock is Empty\n");
}