Beruflich Dokumente
Kultur Dokumente
Linked Lists are another useful data structure. It has great advantage over
Array data structure. In comparison to array it is more advantageous in the
following sense-
In Array as a programmer we have to specify the size/capacity of that
array. This restriction is dangerous whenever we need to maintain extra
record beyond specified size. Also in case of less record then size. In this
case a lot of memory cells are wasted.
If array is full, it is never possible to further store extra records in this array.
IN LINKED LISTS FOLLOWING STEPS ARE
REQUIRED:
• Creation of memory [using malloc ()].
• Collection the base address returned by malloc () into a temporary
pointer variable.
• Storing own data into the information part of the node.
• Now store data into the node.
Store ‘NULL’ into the next part of the node.
• If head contains ‘Null’ then
Head=temp prev info
next
Else c_node-> next=temp
• And at last store temp into c_node.
IMPORTANT TERMS:
Node: The components which form the list.
Temp: Contains the temporary address which is pointer variable.
Head: Contains the address of first node.
C_node: Contains the address of current node.
Next: Contains the address of next node. If there is only one node in the list
then it holds ‘NULL’.
Representation of a Linked List
Operations on a linked list:
Creation of a linked list
Display
Traversing
Insertion
Deletion
Searching
Concatenation
Merging
Types of linked lists:
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
4. Circular Doubly Linked List
2. CIRCULAR LINKED
void main()
{
printf(“Enter Data to fill Circular Linked List:\n”);
scanf(“%d”,&data);
if(head==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->num=data;
temp->next=head;
head=temp;
rear=temp;
}
else
{
temp=(struct node *)malloc(sizeof(struct node));
temp->num=data;
temp->next=head;
rear->next=temp;
rear=temp;
}
}
In doubly linked list, each structure type node contains 3 parts: Info, Next & Prv.
‘Prv’ contains the address of just previous node. ‘Next’ contains the address of just
next node and ‘Info’ contains the information (variable).
/*Creation of Doubly Linked List*/
void create()
{
do{
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter Ur Data”);
scanf(“%d”,&temp->info);
if(front==NULL)
{
temp->next=NULL;
temp->prv=NULL;
rear=front=temp;
cnode=temp;
}
else
{
temp->next=NULL;
temp->prv=cnode;
cnode->next=temp;
cnode=cnode->next;
rear=temp;
}
printf(“\nEnter Y to Continue:”);
ch=getche();
}while(ch==’y’||ch==’Y’);
}
/*Insertion At Front*/
void insert_beg(int data)
{
node *ptr;
prt=(node *)malloc(sizeof(node));
ptr->info=data;
ptr->prv=NULL;
ptr->next=front;
front->prv=ptr;
front=ptr;
}
/*Insertion at the End*/
void insert_end(int data)
{
node *ptr;
ptr=(node *)malloc(sizeof(node));
ptr->info=data;
ptr->next=NULL;
ptr->prv=rear;
rear->next=ptr;
rear=ptr;
}
/*Deletion of a Node from specified position
in Doubly Linked List*/
(i) First Method
void del_spe()
{
struct node *case1,*yahoo,*ali,*ptr;
int data,c=0;
printf(“\nEnter Position of node”);
scanf(“%d”,&data);
case1=front;
if(data==0)
{
ptr=front;
free(ptr);
front=front->next;
front->prv=NULL;
}
else
{
while(case1!=NULL)
{
c++;
if(c==(data-1))
{
ptr=case1;
}
if(c==data)
{
yahoo=case1;
free(yahoo);
}
if(c==(data+1))
ali=case1;
case1=case1->next;
}
ptr->next=ali;
}
}
(ii) Second Method
cnode=front;
while(cnode!=NULL)
{
if(cnode->info==data)Break;
node=node->next;
}
cnode->next->prv=cnode->prv;
cnode->prv->next=cnode->next;
free(cnode);
/*Insertion At beginning*/
void insert_beg()
{
node *ptr,*temp;
int item;
prt=(node *0malloc(sizeof(node));
printf(“\nEnter the No:”);
scanf(“%d”,&item);
ptr->info=item;
temp=head->right;
head->right=ptr;
ptr->left=head;
ptr->right=temp;
temp->left=ptr;
}
/*Insertion At Last*/
void insert_end()
{
node *ptr,*temp;
int item;
ptr=(node *)malloc(sizeof(node));
scanf(“%d”,&ptr->info);
temp=head->left;
temp->right=ptr;
ptr->left=temp;
ptr->right=head;
head->left=ptr;
}
/*Deletion from Beginning*/
void del_beg()
{
node *temp;
if(head->right==head)
{
return;
}
else
{
temp=head->right;
printf(“\deleted element is =%d \n”,temp->info);
head->right=temp->right;
temp->right=temp->left=head;
free(temp);
}
}
/*Delete from Last*/
void del_end()
{
node *temp;
if(head->right==head)return;
else
{
temp=head->left;
printf(“\nDeleted element is %d \n”,temp->info);
head->left=temp->left;
free(temp);
}
Header Linked
List
The Header Linked List is similar to the single linked list but it contains some extra
information. This Header list’s 1st node holds that vita information such as the total
number of nodes.
Creation of a POLYNOMIAL
-----------------------------------------------------------------
void main()
{
int c,e;
struct poly
{
int coef;
int expo;
struct poly *next;
}*temp,*cnode,*head,*ele,*rear;
char ch;
printf(“Enter co-efficient\n”);
printf(“Enter Exponent\n”);
scanf(“%d%d”,&c,&e);
temp=(struct poly *)malloc(sizeof(struct poly));
temp->next=NULL;
if(head==NULL)
{
head=temp;
cnode=temp;
}
else
{ BY SIR
cnode->next=temp;
cnode=cnode->next;
}
printf(“Press Y to continueeeeeee:\n”);
ch=getche();
}while(ch==’y’||ch==’Y’);
-----------------------------------------------------------------
-----------------------------------------------------------------
rear=temp;
for(;;)
{
printf(“Enter co-efficient\n”);
ele=(struct node *)malloc(sizeof(struct node));
scanf(“%d”,&coef);
if(ele->c==0)Break;
printf(“Enter Exponent:\n”);
scanf(“%d”,&e);
ele->expo=e; BY BOOK
if(ele->expo<=0)
{
printf(“INVALID INPUT\n”);
Break;
}
ele->next=NULL;
rear->next=ele;
rear=ele;
}
temp=temp->next;
}
-----------------------------------------------------------------
/*Displaying a Polynomial*/
cnode=head;
Printf(“\n%dx^%d’,cnode->coef,cnode->expo);
cnode=cnode->next;
while(cnode!=NULL)
{
if(cnode->coer>0)
printf(“+”);
else
if(cnode->expo==0)
printf(“%d”,cnode->coef);
printf(“%dx^%d”,cnode->coef,cnode->expo);
cnode=cnode->next;
}
ADDITION
The address of 1st Polynomial is stored in s1 and 2nd Polynomial is stored in s2.
t3=polyadd(s1,s3);
show(t3);
struct poly *polyadd(struct poly *s1,struct poly *s2)
{
struct poly *s3=0,*p3,*tempo;
if(s1==0&&s2==0)return s3;
while(s1!==0&&s2!==0)
{
tempo=(struct poly *)malloc(sizeof(struct poly));
if(s3==0)
{
s3=tempo;
p3=s3;
}
else
{
p3->next=tempo;
p3=p3->next;
}
if(s1->expo>s2->expo)
{
tempo->coef=s1->coef;
tempo->expo=s1->expo;
s1=s1->next;
}
else if(s2->expo>s1->expo)
{
tempo->coef=s2->coef;
tempo->expo=s2->expo;
s2=s2->next;
}
else if(s1->expo==s2->expo)
{
tempo->coef=s1->coef+s2->coef;
tempo->expo=s1->expo;
s1=s1->next;
s2=s2->next;
}
}
while(s1!=0)
{
tempo=(struct node *)malloc(sizeof(struct poly));
tempo->coef=s1->coef;
tempo->expo=s1->expo;
if(s3==0)
{
s3=tempo;
p3=s3;
}
else
{
p3->next=tempo;
p3=p3->next;
}
s1=s1->next;
}
while(s2!=0)
{
tempo=(struct poly *)malloc(sizeof(struct node));
tempo->coef=s2->coef;
tempo->expo=s2->expo;
if(s3==0)
{
s3=tempo;
p3=s3;
}
else
{
p3->next=tempo;
p3=p3->next;
}
p3->next=0;
return t3;
}