260 likes | 473 Views
Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India. UNIT 1. LINKED LIST. START. x. A Linked List is a linear collection of data elements called nodes, where the linear order is given by means of pointers.
E N D
Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India UNIT 1
LINKED LIST START x • 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. 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
START 5 89 0 75 3 78 4 80 1 74 2 LINK LIST REPRESENTATION OF THE MARKS UNIT 1
ALGORITHM FOR TRAVERSING A LINKED LIST • Set PTR=START [Initializes pointer PTR] • Repeat steps 3 & 4 while PTR != NULL • 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) • SET PTR = START • REPEAT STEP 3 WHILE PTR != NULL • 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] • [SEARCH IS UNSUCESSFUL] SET LOC=NULL • EXIT UNIT 1
ALGORITHM FOR SEARCHING IN SORTED LIST • SRCHSL(INFO,LINK,START,ITEM,LOC) • SET PTR = START • REPEAT STEP 3 WHILE PTR != NULL • 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] • [SEARCH IS UNSUCESSFUL] SET LOC=NULL • 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: • Beginning of the list • Between two nodes • 3. In a sorted list UNIT 1
AT BEGINNING OF THE LINKED LIST START x AVAIL x UNIT 1
START x AVAIL x UNIT 1
ALGORITHM FOR INSERTION AT BEGINNING • INSFIRST(INFO,LINK,START,AVAIL,ITEM) • [OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT • [ Remove first node from AVAIL list] set NEW = AVAIL & AVAIL= LINK[AVAIL] • Set INFO[NEW]=ITEM [copies new data into new node] • Set LINK[NEW]= START [new node points to original first node] • Set START= NEW [changes START so it points to the new node] • exit UNIT 1
INSERT AFTER A GIVEN NODE START x Node A Node B AVAIL x UNIT 1
START Node B Node A x AVAIL Node N x UNIT 1
ALGORITHM FOR INSERTION AT POSITION • INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM) • [OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT • [ Remove first node from AVAIL list] set NEW = AVAIL & AVAIL= LINK[AVAIL] • Set INFO[NEW]=ITEM [copies new data into new node] • 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] • 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 item 1 Do you want to continue(type 0 or 1)? 1 Enter the data item 2 Do you want to continue(type 0 or 1)? 1 Enter the data item 3 Do you want to continue(type 0 or 1)? 1 Enter the data item 4 Do you want to continue(type 0 or 1)? 0 Status 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; else ptr->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 UNIT 1