0% found this document useful (0 votes)
61 views7 pages

Mahedi 1.1 1.2

1) The document contains information about a student named Mahedi Hassan with student ID 20183290535 studying Data Structure under teacher Yi. 2) It discusses inserting an element into an array in C by shifting elements and making space at the specified position. 3) It also discusses linked lists as an alternative to arrays that allows dynamic size and easier insertions/deletions, describing the basic structure of linked list nodes and types like singly and doubly linked lists.

Uploaded by

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

Mahedi 1.1 1.2

1) The document contains information about a student named Mahedi Hassan with student ID 20183290535 studying Data Structure under teacher Yi. 2) It discusses inserting an element into an array in C by shifting elements and making space at the specified position. 3) It also discusses linked lists as an alternative to arrays that allows dynamic size and easier insertions/deletions, describing the basic structure of linked list nodes and types like singly and doubly linked lists.

Uploaded by

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

NAME : MAHEDI HASSAN

STUDENT ID : 20183290535

SUBJECT : Data Structure

SCHOOL : Information Science and Engineering

MAJOR : Computer Science & Technology

TEACHER : TEACHER YI
Session 1
Part 1 : ARRAY INSERTION

Introduction

An array is a collection of items stored at contiguous memory locations. In this article, we will
see how to insert an element in an array in C.
Given an array arr of size n, this article tells how to insert an element x in this array arr at a
specific position pos.

Approach:
Here’s how to do it.
1. First get the element to be inserted, say x
2. Then get the position at which this element is to be inserted, say pos
3. Then shift the array elements from this position to one position forward, and do this for all the
other elements next to pos.
4. Insert the element x now at the position pos, as this is now empty.
Code
#include <bits/stdc++.h>
using namespace std;

int main(){

freopen("input.txt",  "r", stdin);


freopen("output.txt", "w", stdout);

int n, arr[10000];
cin >> n;
for (int index=0;index<n;index++){
cin >> arr[index];
}
for (int index=0;index<n;index++){
cout << arr[index] << " ";
}
cout << '\n';
}

Result
Session 1
Part 2 : Linked list insertion

Introduction
One disadvantage of using arrays to store data is that arrays are static structures and
therefore cannot be easily extended or reduced to fit the data set. Arrays are also
expensive to maintain new insertions and deletions. In this chapter we consider
another data structure called Linked Lists that addresses some of the limitations of
arrays.

A linked list is a linear data structure where each element is a separate object.

Each element (we will call it a node) of a list is comprising of two items - the data and
a reference to the next node. The last node has a reference to null. The entry point
into a linked list is called the head of the list. It should be noted that head is not a
separate node, but the reference to the first node. If the list is empty then the head
is a null reference.
A linked list is a dynamic data structure. The number of nodes in a list is not fixed and
can grow and shrink on demand. Any application which has to deal with an unknown
number of objects will need to use a linked list.

One disadvantage of a linked list against an array is that it does not allow direct
access to the individual elements. If you want to access a particular item then you
have to start at the head and follow the references until you get to that item.

Another disadvantage is that a linked list uses more memory compare with an array -
we extra 4 bytes (on 32-bit CPU) to store a reference to the next node.

Types of Linked Lists

A singly linked list is described above

A doubly linked list is a list that has two references, one to the next node and
another to previous node.

Another important type of a linked list is called a circular linked list where last node
of the list points back to the first node (or the head) of the list.

Code
#include<bits/stdc++.h>

using namespace std;

struct node
{
    int data;
    node*next;
};
int main()
{
    int data,i,n,position;
    printf("How many nodes are there: ");
    scanf("%d",&n);

    node*head,*temp,*newnode;

    head=NULL;
    head=new node();

    scanf("%d",&data);
    head->data=data;
    head->next=NULL;

    temp=head;

    for(i=2;i<=n;i++)
    {
        newnode=new node();
        scanf("%d",&data);

        newnode->data=data;
        newnode->next=NULL;

        temp->next=newnode;
        temp=temp->next;
    }
    printf("insert element at your desire point: \n");
    printf("Enter data and position : ");
    scanf("%d %d",&data,&position);
    newnode=new node;

    newnode->data=data;
    newnode->next=head;

    if(position==1)
    {
        head=newnode;
    }
    else
    {
        temp=head;
        for(i=2;i<=position-1;i++)
        {
            temp=temp->next;
            if(temp==NULL)
                break;
        }
        if(temp!=NULL)
        {
            newnode->next=temp->next;
            temp->next=newnode;
        }
    }

    temp=head;

    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->next;
    }

Result

You might also like