unit 1 data structures using c linked list by rohit khokher department of computer science, vidya...
TRANSCRIPT
UNIT 1
Data Structures Using CLinked List
ByRohit Khokher
Department of Computer Science, Vidya College of Engineering, Meerut, India
UNIT 1
LINKED LIST
A Linked List is a linear collection of data elements called nodes, where the linear order is given by means of pointers.
The each node is divided into two parts: the first part contain the information of the element, and the second part, called the link field contains the address of t he next node in the list.
x
START
LINK LIST WITH 4 NODES
UNIT 1
The pointer of the last node contains a special value called a null pointer which is any invalid address.
The value of null character is either ’0’ or ‘any negative value’.
The linked list also contains a list pointer variable called start which contains the address of the first node of the link list.
A special case in a list is that has no node such that list is called a null or empty list and it is represented by the null pointer in the start.
Example:- create a link list for marks of five subject 89,75,78,80,74 in ascending order
UNIT 1
89
75
78
80
74
5
START
2
3
4
1
0
LINK LIST REPRESENTATION OF THE
MARKS
UNIT 1
ALGORITHM FOR TRAVERSING A LINKED LIST
1. Set PTR=START [Initializes pointer PTR]
2. Repeat steps 3 & 4 while PTR != NULL
3. Apply process to INFO[PTR]
4. Set PTR:- LINK[PTR] [PTR now points to next node] [End of step 2 loop] 5. Exit
UNIT 1
PTR
PTR=LINK[PTR]
UNIT 1
ALGORITHM FOR SEARCHING IN A UNSORTED LINKED LIST
SEARCH(INFO,LINK,START,ITEM,LOC)
1. SET PTR = START
2. REPEAT STEP 3 WHILE PTR != NULL
3. IF ITEM = INFO[PTR] then SET LOC = PTR
& EXIT ELSE
SET PTR= LINK[PTR] [PTR now points to the next node] [END OF IF STRUCTURE]
[END OF STEP 2 LOOP]
4. [SEARCH IS UNSUCESSFUL] SET LOC=NULL
5. EXIT
UNIT 1
ALGORITHM FOR SEARCHING IN SORTED LIST
SRCHSL(INFO,LINK,START,ITEM,LOC)1. SET PTR = START2. REPEAT STEP 3 WHILE PTR != NULL3. IF ITEM < INFO[PTR] then;
SET PTR = LINK[PTR] [PTR now points to the next node]
ELSE IF ITEM = INFO [PTR] then;
SET LOC = PTR & EXIT [search is successful] ELSE
SET LOC = NULL & EXIT [item now exceed INFO[PTR]] [END OF IF STRUCTURE] [END OF STEP 2 LOOP]
4. [SEARCH IS UNSUCESSFUL] SET LOC=NULL5. EXIT
UNIT 1
OVERFLOW & UNDERFLOW CONDITIONS
OVERFLOW will occur with our link list when AVAIL= NULL &there is an insertion UNDERFLOW will occur with our link list when START=NULL & there is a deletion.
UNIT 1
INSERTION IN A LINKED LIST
This Can Be Done In Three Ways:
1. Beginning of the list
2. Between two nodes
3. In a sorted list
UNIT 1
AT BEGINNING OF THE LINKED LIST
x
START
x
AVAIL
UNIT 1
x
START
x
AVAIL
UNIT 1
ALGORITHM FOR INSERTION AT BEGINNING
INSFIRST(INFO,LINK,START,AVAIL,ITEM)
1. [OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT2. [ Remove first node from AVAIL list]
set NEW = AVAIL & AVAIL= LINK[AVAIL]3. Set INFO[NEW]=ITEM [copies new data into new node]4. Set LINK[NEW]= START [new node points to original first node]5. Set START= NEW [changes START so it points to the new
node]6. exit
UNIT 1
INSERT AFTER A GIVEN NODE
x
START
x
AVAIL
Node A Node B
UNIT 1
x
START
x
AVAIL
Node A Node B
Node N
UNIT 1
ALGORITHM FOR INSERTION AT POSITION
INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
1. [OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT2. [ Remove first node from AVAIL list]
set NEW = AVAIL & AVAIL= LINK[AVAIL]3. Set INFO[NEW]=ITEM [copies new data into new node]4. If LOC=NULL then [INSERT AS A FIRST NODE]
set LINK[NEW]= START & START=NEW
else [INSERT node after node with location LOC]set LINK[NEW]=LINK[LOC] &
LINK[LOC]=NEW;[END OF IF
STRUCTURE]5. exit
UNIT 1
CREATION OF LINKED LIST
#include<stdio.h>#include<conio.h>#include<malloc.h>void main(){struct node{int num;struct node *ptr;};typedef struct node NODE;NODE *head, *first, *temp;int count=0;int choice=1;first=NULL;while(choice){
head=(NODE *)malloc(sizeof(NODE));printf("Enter the data item\n");scanf("%d",&head->num); if(first!=NULL)
{ temp->ptr=head; temp=head;}
UNIT 1
else{ first=temp=head;}
fflush(stdin);printf("Do you want to continue(type 0 or 1)?\n");scanf("%d",&choice);}
temp->ptr=NULL;temp=first;printf("Status of the linked list is\n");while(temp!=NULL)
{printf("%d",temp->num);count++;temp=temp->ptr;
}printf("NULL");printf("NO of nodes in the list =%d\n",count);getch();}
UNIT 1
Output of the Program
Enter the data item1Do you want to continue(type 0 or 1)?1Enter the data item2Do you want to continue(type 0 or 1)?1Enter the data item3Do you want to continue(type 0 or 1)?1Enter the data item4Do you want to continue(type 0 or 1)?0Status of the linked list is
1234NULLNO of nodes in the list =4
UNIT 1
Insertion in Linked List
#include<stdio.h>#include<conio.h>#include<malloc.h>struct node{int info;struct node *next;};typedef struct node NODE;NODE *start;void createmptylist(NODE *start){*start=(NODE *)NULL;}void traversinorder(NODE *start){while(start != (NODE *) NULL){printf("%d\n",start->info);start=start->next;}}
UNIT 1
void insertatbegin(int item){NODE *ptr;ptr=(NODE *)malloc(sizeof(NODE));ptr->info=item;if(start==(NODE *)NULL)ptr->next=(NODE *)NULL;elseptr->next=start;start=ptr;}void insert_at_end(int item){NODE *ptr,*loc;ptr=(NODE *)malloc(sizeof(NODE));ptr->info=item;ptr->next=(NODE *)NULL;if(start==(NODE*)NULL)start=ptr;
UNIT 1
else{loc=start;while(loc->next!=(NODE *)NULL)loc=loc->next;loc->next=ptr;}}void insert_spe(NODE *start,int item){NODE *ptr,*loc;int temp,k;for(k=0,loc=start;k<temp;k++){loc=loc->next;if(loc==NULL){printf("node in the list at less than one\n");return;}}
UNIT 1
ptr=(NODE *)malloc(sizeof(NODE));ptr->info=item;ptr->next=loc->next;;loc->next=ptr;}void main(){int choice,item,after;char ch;clrscr();createmptylist(start);do{printf("1.Insert element at begin \n");printf("2. insert element at end positon\n");printf("3. insert specific the position\n");printf("4.travers the list in order\n");printf("5. exit\n");
UNIT 1
printf("enter your choice\n");scanf("%d",&choice);switch(choice){case 1: printf("Enter the item\n");
scanf("%d",&item);insertatbegin(item);break;
case 2: printf("Enter the item\n");scanf("%d",&item);insert_at_end(item);break;
case 3: printf("Enter the item\n");scanf("%d",&item);insert_spe(start,item);break;
UNIT 1
case 4: printf("\ntravers the list\n");traversinorder(start);break;
case 5: return;}fflush(stdin);printf("do your want continous(y for yes)\n");scanf("%c",&ch);}while((ch='y')||(ch='Y'));getch();}
UNIT 1
THANKS