Question

In: Computer Science

A data structure can be defined as a mechanism for organizing the data a program stores...

A data structure can be defined as a mechanism for organizing the data a program stores in memory. An example of a data structure is a Linked List. Discuss how a linked list works and the methods of this data structure. (Please be super detailed and type the solution)

Solutions

Expert Solution

Linked List:

Linked list is a data structure which completely deals with dynamic memory allocation.It does not follow contiguous memory allocation like arrays.In order to use the memory space efficiently,Linked list has invented.

Generally linked lists are of two types:

1.Single linked list:

The basic structure in the linked list is node.

      Data     Pointer to next node

The above figure shows the node structure in a linked list.

First field contains data of this node,and Second field contains Link to next node.

Single linked list supports one way traversal only i.e.,from either left to right or right to left.

Operations on single linked list:

1.We can create a node at beginning.

2.We can add a node at ending.

3.We can add a node at required location.

4.We can find length ;.e.,Number of nodes in the linked list.

5.We can display the data of the linked list.

6.We can Delete the node from beginning.

7.We can Delete the node from ending.

8.We can Delete the node from particular loction.

9.We can sort the data.

10.We can reverse the data linked list.

Program code for Single Linked List in 'C'(Integer data):

#include<stdio.h>
#include<conio.h>
void display();
int length();
void addatbegin();
void addatloc();
void addatend();
void deleteatbeg();
void deleteatend();
void deleteatloc();
void sort();
void reverse();
struct node
{
int data;
struct node *link;
};
struct node *root=NULL;
void main()
{
int n,d,i=1;
clrscr();
printf("This is a programme by PRAVEENA on all the operations of Single Linked List.");
while(i)
{
printf("\n\n\nSingle linked list menu:");
printf("\n1.Add at beginning");
printf("\n2.Add at ending");
printf("\n3.Add at particular location.");
printf("\n4.Delete at beginning.");
printf("\n5.Delete at ending.");
printf("\n6.Delete at particular location.");
printf("\n7.Display");
printf("\n8.Length");
printf("\n9.Sorting");
printf("\n10.Reverse");
printf("\n11.Exit");
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:addatbegin();
       break;
case 2:addatend();
       break;
case 3:addatloc();
       break;
case 4:deleteatbeg();
       break;
case 5:deleteatend();
       break;
case 6:deleteatloc();
       break;
case 7:display();
       break;
case 8:d=length();
       printf("Length is:%d",d);
       break;
case 9:sort();
       break;
case 10:reverse();
       break;
case 11:i=0;
       break;
default:printf("wrong choice.");
}
}
getch();
}
void addatbegin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
if(root==NULL)
{
root=temp;
temp->link=NULL;
}
else
{
struct node *p;
p=root;
root=temp;
root->link=p;
}
}
void display()
{
struct node *p;
p=root;
while(p!=NULL)
{
printf("%3d",p->data);
p=p->link;
}
}
int length()
{
struct node *p;
int c=0;
p=root;
while(p!=NULL)
{
p=p->link;
c++;
}
return c;
}
void addatend()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node *p;
p=root;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}
}
void addatloc()
{
struct node *temp;
int loc,i=1;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
printf("Enter location:");
scanf("%d",&loc);
if(loc>length()+1)
{
printf("Invalid location:");
}
else
{
if(loc<length()+1)
{
struct node *p;
p=root;
while(i<loc-1)
{
p=p->link;
i++;
}
temp->link=p->link;
p->link=temp;
}
if(loc==1)
{
addatbegin();
}
if(loc==length()+1)
{
addatend();
}
}
}
void deleteatend()
{
struct node *p,*q;
p=root;
while(p->link!=NULL)
{
q=p;
p=p->link;
}
q->link=NULL;
p=NULL;
free(p);
}
void deleteatbeg()
{
struct node *temp;
temp=root;
root=temp->link;
free(temp);
}
void deleteatloc()
{
struct node *p,*q;
int loc,i=1;
printf("Enter location:");
scanf("%d",&loc);
if(loc>length())
{
printf("Invalid location.");
}
else
{
if(loc==1)
{
addatbegin();
}
else
{
p=root;
while(i<loc)
{
q=p;
p=p->link;
i++;
}
q->link=p->link;
p->link=NULL;
free(p);
}
}
}
void sort()
{
struct node *p,*q;
int temp=0;
if(root==NULL)
{
printf("There are no nodes to sort.");
}
else
{
printf("Elements after sorting is:");
for(p=root;p!=NULL;p=p->link)
{
for(q=p->link;q!=NULL;q=q->link)
{
if((p->data)>(q->data))
{
temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
display();
}
}
void reverse()
{
int i,j,k,l;
struct node *p,*q;
int temp=0;
q=root;
p=root;
l=length();
i=0;
j=l-1;
if(root==NULL)
{
printf("Linked list is empty.");
}
else
{
printf("Elements after reversing is:");
while(i<j)
{
k=0;
while(k<j)
{
q=q->link;
k++;
}
temp=p->data;
p->data=q->data;
q->data=temp;
i++;
j--;
p=p->link;
q=root;
}
display();
}
}

2.Double linked list:

The only difference between single and double linked list is we can traverse in both ways in Double linked list as it's node contains link to it's right and left nodes..

   Link to left node              Data    Link to right node

It can perform all operations performed by single linked list.

Program code for Double linked list in 'C'(Integer data):

#include<stdio.h>
#include<conio.h>
void display();
int length();
void addatbegin();
void addatloc();
void addatend();
void deleteatbeg();
void deleteatend();
void deleteatloc();
struct node
{
int data;
struct node *rlink;
struct node *llink;
};
struct node *root=NULL;
void main()
{
int n,d,k=1;
clrscr();
printf("\n\nThis is a programme by PRAVEENA on Double Linked List\n\n");
while(k)
{
printf("\n\nDouble linked list menu:");
printf("\n1.Add at beginning");
printf("\n2.Add at ending");
printf("\n3.Add at particular location.");
printf("\n4.Delete at beginning.");
printf("\n5.Delete at ending.");
printf("\n6.Delete at particular location.");
printf("\n7.Display");
printf("\n8.Length");
printf("\n9.Exit");
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
addatbegin();
break;
case 2:
addatend();
break;
case 3:
addatloc();
break;
case 4:
deleteatbeg();
break;
case 5:
deleteatend();
break;
case 6:
deleteatloc();
break;
case 8 :
d=length();
printf("Length of the double linked list is:%d",d);
break;
case 7:
display();
break;
case 9:
k=0;
break;
default:
printf("wrong choice.");
}
}
getch();
}
void addatend()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter node data:");
scanf("%d",&temp->data);
if(root==NULL)
{
root=temp;
root->llink=NULL;
root->rlink=NULL;
}
else
{
struct node *p;
p=root;
while(p->rlink!=NULL)
{
p=p->rlink;
}
p->rlink=temp;
temp->llink=p;
temp->rlink=NULL;
}
}
void addatloc()
{
int loc,i=1;
printf("Enter location:");
scanf("%d",&loc);
if(loc>length()+1)
{
printf("Invalid location.(Enter the location which is less than %d).",length()+2);
}
if(loc<=length())
{
if(loc==1)
{
addatbegin();
}
else
{
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
p=root;
while(i<loc-1)
{
p=p->rlink;
i++;
}
temp->rlink=p->rlink;
p->rlink->llink=temp;
p->rlink=temp;
temp->llink=p;
}
}
if(loc==length()+1)
{
addatend();
}
}
void addatbegin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->llink=NULL;
if(root==NULL)
{
root=temp;
root->rlink=NULL;
}
else
{
temp->rlink=root;
root->llink=temp;
root=temp;
}
}
int length()
{
int c=0;
struct node *p;
p=root;
while(p!=NULL)
{
p=p->rlink;
c++;
}
return c;
}
void display()
{
struct node *p;
p=root;
if(root==NULL)
{
printf("Double Linked list is empty");
}
else
{
printf("Elements in the Double linked list are:");
while(p!=NULL)
{
printf("%3d",p->data);
p=p->rlink;
}
}
}
void deleteatend()
{
struct node *p,*q;
p=root;
while(p->rlink!=NULL)
{
q=p;
p=p->rlink;
}
printf("Deleted node data is:%d",p->data);
q->rlink=NULL;
p->llink=NULL;
free(p);
}
void deleteatbeg()
{
struct node *p;
p=root;
if(root==NULL)
{
printf("Sorry!!!There is no node to delete.");
}
else
{
printf("Deleted node data is:%d",p->data);
root=p->rlink;
root->llink=NULL;
free(p);
}
}
void deleteatloc()
{
struct node *p,*q;
int i=1,loc;
printf("Enter location:");
scanf("%d",&loc);
if(loc>length())
{
printf("Invalid node(Enter the number which is less than %d)",length()+1);
}
if(loc==length())
{
deleteatend();
}
if(loc<length())
{
if(loc==1)
{
deleteatbeg();
}
else
{
p=root;
while(i<loc)
{
q=p;
i++;
p=p->rlink;
}
printf("Deleted node data is:%d",p->data);
q->rlink=p->rlink;
p->rlink->llink=q;
}
}
}

3.Circular linked list:

In single and double linked lists,The last node address part contains null as there is no next node to point out.

But in circular linked list,The last node point out to the first node of linked list.

It can be implemented using either Single Linked list or double linked list.

It can perform all operations performed by Single and Double Linked lists.

Program code for Circular Linked List in 'c'(Using Single Linked List)(Integer data):

#include<stdio.h>
#include<conio.h>
void addatbeg();
void addatend();
void delatbeg();
void delatend();
int length();
void display();
struct node
{
int data;
struct node *link;
};
struct node *root=NULL;
void main()
{
int n,p,i=1;
clrscr();
while(i)
{
printf("\n\n\nThis is a programme by PRAVEENA on Circular linked List\n\n");
printf("Circular linked List Menu:");
printf("\n1.Add at beginning");
printf("\n2.Add at Ending");
printf("\n3.Delete at beginning");
printf("\n4.Delete at Ending");
printf("\n5.Length");
printf("\n6.Display");
printf("\n7.Exit");
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:addatbeg();
       break;
case 2:addatend();
       break;
case 3:delatbeg();
       break;
case 4:delatend();
       break;
case 5:p=length();
       printf("Length of the CLL is:%d",p);
       break;
case 6:display();
       break;
case 7:i=0;
       break;
default:printf("Wrong choice.");
}
}
getch();
}
void addatbeg()
{
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter node data:");
scanf("%d",&temp->data);
if(root==NULL)
{
root=temp;
temp->link=root;
}
else
{
p=root;
while(p->link!=root)
{
p=p->link;
}
p->link=temp;
temp->link=root;
root=temp;
}
}
void addatend()
{
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter node data:");
scanf("%d",&temp->data);
if(root==NULL)
{
root=temp;
temp->link=root;
}
else
{
p=root;
while(p->link!=root)
{
p=p->link;
}
p->link=temp;
temp->link=root;
}
}
void delatbeg()
{
struct node *p,*q;
if(root==NULL)
{
printf("Circular linked List is empty.");
}
else
{
p=root;
while(p->link!=root)
{
p=p->link;
}
q=root;
printf("Deleted element is :%d",q->data);
root=q->link;
p->link=root;
q->link=NULL;
free(q);
}
}
void delatend()
{
struct node *p,*q;
if(root==NULL)
{
printf("Circular linked List is empty.");
}
else
{
p=root;
while(p->link!=root)
{
q=p;
p=p->link;
}
printf("Deleted element is:%d",p->data);
q->link=root;
p->link=NULL;
free(p);
}
}
int length()
{
struct node *p;
int c=0;
p=root;
if(root==NULL)
{
printf("Circular linked list is empty.");
return 0;
}
else
{
c++;
while(p->link!=root)
{
c++;
p=p->link;
}
return c;
}
}
void display()
{
struct node *p;
if(root==NULL)
{
printf("There is no node to display.");
}
else
{
p=root;
printf("Data in the Circular linked List are:");
printf("%3d",p->data);
p=p->link;
while(p!=root)
{
printf("%3d",p->data);
p=p->link;
}
}
}


Related Solutions

Information architecture can be simply defined as organizing shared information. There are many different ways to...
Information architecture can be simply defined as organizing shared information. There are many different ways to organize web sites, including by audience, task, or topic. Discuss how a web site that you visit often is organized by audience, task, or topic. Provide a link to the site and support your argument with specific examples from the site.
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
what is the difference between organizing function and organizational structure and are they related?
what is the difference between organizing function and organizational structure and are they related?
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
Explain the mechanism that creates the hyperfine structure of hydrogen.
Explain the mechanism that creates the hyperfine structure of hydrogen.
1. Briefly explain why the market system is considered an organizing mechanism. 2. How does "consumer...
1. Briefly explain why the market system is considered an organizing mechanism. 2. How does "consumer sovereignty" determine the types and quantities of the goods produced in a market economy?
Using the data set as a pre-defined variable in your program, write code that uses the...
Using the data set as a pre-defined variable in your program, write code that uses the dataset to print the first names of people who have BOTH above average math grades AND below average age from the dataset. The solutions for the textbook examples assume you are able to export in a framework like node.js, which is why in the data set I provide, I simply set the array of objects as a variable. Your code will be in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT