0% found this document useful (0 votes)
33 views11 pages

Traversing A Single-Dimensional Array Definition

The document covers traversing and searching elements in single-dimensional arrays and linked lists, including circular linked lists, with example C programs. It explains how to access elements sequentially, perform linear searches, and highlights the manual specification of array sizes instead of using sizeof(). Each section includes code snippets and detailed explanations of the processes involved.

Uploaded by

pratimdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Traversing A Single-Dimensional Array Definition

The document covers traversing and searching elements in single-dimensional arrays and linked lists, including circular linked lists, with example C programs. It explains how to access elements sequentially, perform linear searches, and highlights the manual specification of array sizes instead of using sizeof(). Each section includes code snippets and detailed explanations of the processes involved.

Uploaded by

pratimdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Traversing a Single-Dimensional Array

Definition:
Traversing an array means accessing each element sequentially, either for processing, modification,
or display.

Example Program: Traversing an Array

#include <stdio.h>

int main() {

int a[] = {10, 20, 30, 40, 50};

int n = sizeof(a) / sizeof(a[0]);

int i;

printf("Array elements: ");

for(i = 0; i < n; i++) {

printf("%d ", a[i]);

return 0;

Explanation:

1. We initialize an array a[] with 5 elements.

2. We determine its size using sizeof(a) / sizeof(a[0]).

3. We use a loop to traverse the array and print each element.

2. Traversing a Single-Dimensional Linked List

Definition:
A linked list consists of nodes, where each node contains:

• Data

• A pointer to the next node

Example Program: Traversing a Linked List

#include <stdio.h>

#include <stdlib.h>
struct Node {

int data;

struct Node* next;

};

void traverse(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

int main() {

struct Node* head = (struct Node*)malloc(sizeof(struct Node));

struct Node* second = (struct Node*)malloc(sizeof(struct Node));

struct Node* third = (struct Node*)malloc(sizeof(struct Node));

head->data = 10;

head->next = second;

second->data = 20;

second->next = third;

third->data = 30;

third->next = NULL;

printf("Linked List elements: ");

traverse(head);
return 0;

Explanation:

1. We define a Node structure with an integer data and a pointer next.

2. We dynamically allocate memory for three nodes.

3. The traverse() function iterates through the list using a loop.

3. Searching for an Element in a Single-Dimensional Array or Linked List

A. Searching in an Array (Linear Search)

#include <stdio.h>

int main() {

int a[] = {10, 20, 30, 40, 50};

int n = sizeof(a) / sizeof(a[0]);

int key = 30, i, found = 0;

for(i = 0; i < n; i++) {

if (a[i] == key) {

found = 1;

break;

if (found)

printf("Element %d found at index %d\n", key, i);

else

printf("Element not found\n");

return 0;

Explanation:
• We iterate through the array and compare each element with key.

• If found, we print its index.

B. Searching in a Linked List

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

int search(struct Node* head, int key) {

struct Node* temp = head;

while (temp != NULL) {

if (temp->data == key)

return 1;

temp = temp->next;

return 0;

int main() {

struct Node* head = (struct Node*)malloc(sizeof(struct Node));

struct Node* second = (struct Node*)malloc(sizeof(struct Node));

struct Node* third = (struct Node*)malloc(sizeof(struct Node));

head->data = 10; head->next = second;

second->data = 20; second->next = third;

third->data = 30; third->next = NULL;


int key = 20;

if (search(head, key))

printf("Element %d found in linked list\n", key);

else

printf("Element not found\n");

return 0;

Explanation:

• We traverse the linked list and compare data with key.

• If found, we return 1; otherwise, we return 0.

4. Circular Linked List

Definition:
A circular linked list is a variation of a linked list where:

• The last node points to the first node.

Example Program: Traversing a Circular Linked List

CopyEdit

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void traverse(struct Node* head) {

if (head == NULL) return;

struct Node* temp = head;

do {
printf("%d ", temp->data);

temp = temp->next;

} while (temp != head);

int main() {

struct Node* head = (struct Node*)malloc(sizeof(struct Node));

struct Node* second = (struct Node*)malloc(sizeof(struct Node));

struct Node* third = (struct Node*)malloc(sizeof(struct Node));

head->data = 10; head->next = second;

second->data = 20; second->next = third;

third->data = 30; third->next = head; // Circular connection

printf("Circular Linked List elements: ");

traverse(head);

return 0;

Explanation:

• We create three nodes and link them circularly.

• In traverse(), we use a do-while loop to avoid an infinite loop.

This covers all four topics in detail with example programs in C. Let me know if you need
modifications or explanations!

1. Traversing a Single-Dimensional Array (Without sizeof)

We assume the number of elements (n) is known beforehand.

CopyEdit

#include <stdio.h>
int main() {

int a[] = {10, 20, 30, 40, 50};

int n = 5; // Manually specify the array size

int i;

printf("Array elements: ");

for(i = 0; i < n; i++) {

printf("%d ", a[i]);

return 0;

Explanation:

• We manually set n = 5 since sizeof() is not used.

• We loop from 0 to n-1 to print elements.

2. Searching for an Element in an Array (Without sizeof)

#include <stdio.h>

int main() {

int a[] = {10, 20, 30, 40, 50};

int n = 5; // Manually specify array size

int key = 30, i, found = 0;

for(i = 0; i < n; i++) {

if (a[i] == key) {

found = 1;

break;

}
if (found)

printf("Element %d found at index %d\n", key, i);

else

printf("Element not found\n");

return 0;

Explanation:

• We manually specify n = 5 to avoid using sizeof().

• The loop searches for key in the array.

All other linked list programs remain unchanged since sizeof() is not used in them. Let me know if
you need further modifications!

1. Traversing a Single-Dimensional Array (Without sizeof)

Problem Understanding:

In C, an array is a fixed-size data structure where elements are stored in contiguous memory
locations.
To traverse an array, we need to access each element one by one using a loop.

Code:

#include <stdio.h>

int main() {

int a[] = {10, 20, 30, 40, 50}; // Array with 5 elements

int n = 5; // Manually specifying the size of the array

int i; // Loop variable

printf("Array elements: ");

for(i = 0; i < n; i++) { // Looping through the array

printf("%d ", a[i]); // Printing each element

}
return 0;

Step-by-Step Explanation:

1. Declaring the array:

o We define an integer array a[] = {10, 20, 30, 40, 50} with 5 elements.

o The indices of the array elements are:

a[0] = 10, a[1] = 20, a[2] = 30, a[3] = 40, a[4] = 50

2. Manually specifying the size:

o Unlike using sizeof(a) / sizeof(a[0]), we manually assign the array size: n = 5.

3. Looping through the array:

o We use a for loop that starts from i = 0 and goes up to i < n (i.e., 4).

o Inside the loop, we print a[i] using printf("%d ", a[i]);.

4. Output:

Array elements: 10 20 30 40 50

2. Searching for an Element in an Array (Without sizeof)

Problem Understanding:

• Searching means finding whether a specific key is present in the array or not.

• Since we do not use sizeof(), we manually set the array size.

Code:

#include <stdio.h>

int main() {

int a[] = {10, 20, 30, 40, 50}; // Array with 5 elements

int n = 5; // Manually specifying the array size

int key = 30; // Element to search for

int i, found = 0; // 'found' is used to track if key is present

for(i = 0; i < n; i++) { // Loop through the array

if (a[i] == key) { // Check if the current element matches the key


found = 1; // Set found to true

break; // Exit the loop since we found the key

if (found) // If key is found

printf("Element %d found at index %d\n", key, i);

else

printf("Element not found\n");

return 0;

Step-by-Step Explanation:

1. Declaring the array and size:

o The array a[] = {10, 20, 30, 40, 50} has 5 elements.

o We manually specify n = 5 instead of sizeof(a)/sizeof(a[0]).

2. Defining the key to search:

o The key variable is set to 30, which we want to find.

3. Linear Search Logic:

o We loop through the array from i = 0 to i < n.

o At each iteration, we check if a[i] == key.

o If found, we:

▪ Set found = 1

▪ Store the index i

▪ Break the loop to stop further unnecessary checks.

4. Checking the result:

o If found == 1, print the key and its index.

o Otherwise, print "Element not found".

5. Output (if key = 30):

Element 30 found at index 2

6. Edge Cases Considered:


o If key = 100 (not in the array), the output will be:

Element not found

Why Not Use sizeof()?

In both programs, we manually specify the size (n = 5) instead of computing it using sizeof().
This is useful when:

1. The array is declared globally or dynamically allocated.

2. The array size is known beforehand (e.g., hardcoded values).

You might also like