Вы находитесь на странице: 1из 18

Circular Linked List : (CLL)

The Last node in the list points to the first node. (ie.,) The last node pointer field contains the address of the first node.

Doubly Linked List : (DLL)


Each node of DLL consists of two pointers, which holds the

address of the previous node and next node. Therefore, it enables bidirectional traversing. (ie.,) it traverse both forward and backward. The pointer points to the previous node are called Left link

(llink) and pointer points to the next node are called right link (rlink)

//DOUBLY LINKED LIST EXAMPLE #include<stdio.h> #include<conio.h> struct emp { int eno; char ename[25]; int sal; struct emp *next; struct emp *prev; }; struct emp *new1=NULL,*start=NULL,*temp=NULL,*last=NULL,*insptr=NULL,*dptr; void accept() { new1=(struct emp*) malloc(sizeof(struct emp)); printf("\n Enter the Employee Number"); scanf("%d",&new1->eno); printf("\n Enter the Employee Name"); scanf("%s",&new1->ename); printf("\n Enter the Employee Salary"); scanf("%d",&new1->sal); }

Continued,

void insert() { insptr=(struct emp*) malloc(sizeof(struct emp)); printf("\n Enter the Employee Number"); scanf("%d",&insptr->eno);

printf("\n Enter the Employee Name"); scanf("%s",&insptr->ename);


printf("\n Enter the Employee Salary"); scanf("%d",&insptr->sal); } void main() { int recno; char ch='y'; clrscr(); while(ch=='y') {
Continued,

accept(); fflush(stdin); printf("\n Do you wish to continue [y/n]"); scanf("%c",&ch); if(start==NULL) { start=new1; temp=new1; new1->prev=NULL; new1->next=NULL; } else { temp->next=new1; new1->prev=temp; new1->next=NULL; temp=new1; } }

Continued,

last=new1; printf("\n Printing the datas in the Stack Form"); for(new1=last;new1!=NULL;new1=new1->prev) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } printf("\n Printing the datas in the Queue Form"); for(new1=start;new1!=NULL;new1=new1->next) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } // Insert the record printf("\n Enter the record number to be inserted"); scanf("%d",&recno); insert(); for (new1=start;new1!=NULL;new1=new1->next) { if (start->eno==recno)
Continued,

{ insptr->next=new1; insptr->prev=NULL; new1->prev=insptr; start=insptr; } else if(new1->eno==recno&&new1->next!=NULL) { temp->next=insptr; insptr->next=new1; new1->prev=insptr; insptr->prev=temp; } else if(new1->eno==recno&&new1->next==NULL) { new1->next=insptr; insptr->next=NULL; insptr->prev=new1; last=insptr; } temp=new1; } Continued,

printf("\n Printing the datas in the After Insertion Stack Form"); for(new1=last;new1!=NULL;new1=new1->prev) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } printf("\n Printing the datas in the Queue Form"); for(new1=start;new1!=NULL;new1=new1->next) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } // Deletion printf("\n Enter the record number to be Deleted"); scanf("%d",&recno); for (new1=start;new1!=NULL;new1=new1->next) { if (start->eno==recno) {

Continued,

start=start->next; start->prev=NULL; dptr=NULL; } else if(new1->eno==recno && new1->next!=NULL) { dptr=new1; temp->next=new1->next; new1->next->prev=temp; free(dptr); dptr=NULL; } else if(new1->eno==recno&&new1->next==NULL) { dptr=new1; temp->next=NULL; last=temp; free(dptr); } temp=new1; }
Continued,

printf("\n Printing the datas in the After Insertion Stack Form"); for(new1=last;new1!=NULL;new1=new1->prev) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } printf("\n Printing the datas in the Queue Form"); for(new1=start;new1!=NULL;new1=new1->next) { printf("%d %s %d\n",new1->eno,new1->ename,new1->sal); } getch();

// Circular linked list using LIFO(stack) method #include<stdio.h> #include<conio.h> #include<process.h> #include<alloc.h> #include<stdlib.h> typedef struct stack { int data; struct stack *next; }node; struct node *head,*top; void main() { int data,item,choice; char ans,ch; void push(int,node **); void display(node **); int pop(node **);
Continued,

int sempty(node *); clrscr(); top=NULL; printf("\n\n\t Stack using Linked List"); do { printf("\n \n The main menu "); printf("\n 1.Push\n 2.Pop \n 3.Display \n 4.Exit"); printf("\n Enter your choice"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the data"); scanf("%d",&data); push(data,&top); break; case 2: if(sempty(top)) printf("\n stack underflow");
Continued,

else { item=pop(&top); printf("\n The popped node is %d",item); } break; case 3:

Display(&top); break;
case 4: printf("\n Do U want to quit?[Y/n]"); ch=getche(); if(ch=='y') exit(0); else break; } printf("\n Do U want to continue?"); ans=getche(); getch(); clrscr(); }while(ans=='y' || ans=='Y'); getch();

Continued,

// Push the data items void push(int item,node **top) { node *new,*next=NULL; node *get_node(int); new=get_node(item); if(*top ==NULL) head=new; // marking Ist node in the stack new->next=*top; *top=new; head->next=*top; } node *get_node(int item) { node *temp; temp=(node *) malloc(sizeof(node)); if(temp==NULL) printf("\n Memory cannot be allocated"); temp->data=item; temp->next=NULL; return(temp);
}
Continued,

int sempty(node *temp) { if(temp==NULL) return 1; else return 0; }

int pop(node **top) { int item; node *temp; item=(*top)->data; temp=*top; if(temp->next==*top) *top=NULL; *top=(*top)->next; head->next=*top; free(temp); return(item); }
Continued,

void display(node **top) { node *temp; temp=*top; if(sempty(temp)) printf("\n Stack is empty"); else { while(temp->next != *top); { printf("%d\n",temp->data); temp=temp->next; } printf("%d\n",temp->data); } getch(); }

Session Summary
The logical organisation of data for easier manipulation is called as a datastructure. The data structure formed when the number of data items are known in advance is referred as Static Data structure. Dynamic data structure can grow or shrink during their execution time.
Some of the Dynamic data structures includes linked lists, stacks, queues and Binary

trees NULL address is the address stored by the Null pointer in the last node of the list A single linked list is a list which each node as a single link to its next node. A double linked list is a list in which each node as two links one its next node and the other to its previous node. A circlular linked list is a list in which each node is connected to the next node as in the case of a singular list except that the list has no end.(.: The last node is connected to the first node of the list)

EXERCISES
1. Write a program in C to create a Single linked list to read a set of N numbers and print the list? 2. Write a program in C to create a Single linked list to read a set of N numbers

and print the list in ascending order?


3. Write a program to create,insert,delete and modify the linkedlist which contain the structure a) An integer quantity named empno b) An array of characters named empname c) An array of characters named deptname d) An floating point quantity named salary

Вам также может понравиться