You are on page 1of 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