0% found this document useful (0 votes)
93 views3 pages

Data - Structure Lab - 04

The document describes a data structures lab assignment to implement a singly linked list with various operations like insert, show, search, delete, and get length. It provides code for a linked list class with these functions and a main method to test it with a menu-driven program. It also asks students to update the destructor to delete all remaining nodes as well as the dummy head node.

Uploaded by

Ameek ghuri
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)
93 views3 pages

Data - Structure Lab - 04

The document describes a data structures lab assignment to implement a singly linked list with various operations like insert, show, search, delete, and get length. It provides code for a linked list class with these functions and a main method to test it with a menu-driven program. It also asks students to update the destructor to delete all remaining nodes as well as the dummy head node.

Uploaded by

Ameek ghuri
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/ 3

United International University (UIU)

Dept. of Computer Science and Engineering


Data Structures Laboratory
Course Code: CSI-218
Semester: summer-2014
Lab – 04

Solve following Programming Problems:

1. Implement a singly linked list (using dummy node) that consists of following operations:

a. Insert (after dummy node or head)


b. Show all
c. Search(search for a specific integer data)
d. Delete (find it and delete)
e. Length (current length of the list)

[Make it Menu driven: 1 means insert, 2 means show, 3 means search and so on….]

2. What are the advantages and disadvantages of Linked list over Array (Continuous list)?

Implementation:
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
node *next;
};

struct list
{
node *head;
int len;

list( )
{
head = new node;
head->next = NULL;
len = 0;
}

~list( )
{
delete head;
}

void insert(node *x) // after head


{
x->next = head->next;
head->next = x;
len++;
}

void show( )
{
node *cur = head;

while(cur->next != NULL)
Page 1 of 3
{
printf("Data : %d\n",(cur->next)->data);
cur = cur->next;
}
}

node* search(int key)


{
node *cur = head;

while(cur->next != NULL)
{
if(cur->next->data == key)return cur->next;
cur = cur->next;
}
return NULL;
}

void remove(int key) // delete from anywhere


{
node *cur = head;

while(cur->next != NULL)
{
if(cur->next->data == key)break;
cur = cur->next;
}

if(cur->next != NULL)
{
node *x = cur;
node *y = cur->next;
node *z = cur->next->next;
x->next = z;
len--;
delete y;
printf("Data removed from the list.\n");
}
else printf("Data not found...\n");
}

int length( )
{
return len;
}

};

int main()
{
int a;
int ch;
list ob;
node *y;

while(1)
{
printf("------MENU------\n");
printf("1) Insert\n");
printf("2) ShowAll\n");
printf("3) Search\n");

Page 2 of 3
printf("4) Remove\n");
printf("5) Length\n");
printf("6) Exit\n");
printf("----------------\n");

printf("Enter Choice:");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("Enter an integer number: ");
scanf("%d",&a);
y = new node;
y->data = a;
ob.insert(y);
printf("Data Inserted.....\n");
break;

case 2:
ob.show();
break;

case 3:
printf("Enter a data(any integer) to search:\n");
scanf("%d",&a);

y = ob.search(a);
if(y == NULL)printf("Data not found...\n");
else printf("Data found: %d\n",y->data);
break;

case 4:
printf("Enter a data(any integer) to delete:\n");
scanf("%d",&a);
ob.remove(a);
break;

case 5:
a = ob.length();
printf("Length of the list is %d.\n",a);
break;

case 6:
exit(0);

default:
printf("Wrong input\n");
}
}
return 0;
}

Class Task
Update the list destructor function in such a way that it can delete all the remaining nodes as well as the
dummy head. [5 Marks]

Page 3 of 3

You might also like