Upcoming SlideShare
×

# Unit7 jwfiles

764 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
764
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
43
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Unit7 jwfiles

1. 1. Data Structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database. Many ways are formulated to handle data efficiently. -- An User-defined data type is a combination of different primary data types, which represents a complex entity. -- An Abstract Data Type ( A D T ) not only represents a set of complex data objects, but also includes a set of operations to be performed on these objects, defines that how the data objects are organized. -- The group of methods implements a set rules, which defines a logical way of handling data. -- The complex entity along with its group of methods is called Abstract Data Type ( A D T ) . -- Data structure is described as an instance of Abstract Data Type ( ADT ). -- We can define that Data structure is a kind of representation of logical relationship between related data elements. In data structure, decision on the operations such as storage, retrieval and access must be carried out between the logically related data elements. Data Structure Some Data structures Arrays Strings Linear Non-Linear Lists Stacks QueuesLinear Lists Stacks Queues Trees Graphs Trees Graphs Some Common Operations on Data structures DictionariesInsertion : adding a new element to the collection. MapsDeletion : removing an element from a collection. Hash TablesTraversal : access and examine each element in collection. SetsSearch : find whether an element is present or not. LatticeSorting : rearranging elements in a particular order. Neural-NetsMerging : combining two collections into one collection.
3. 3. struct node { int rollno; struct node *next; Creating a Singly Linked List}; /* deleting n2 node */int main() { n1->next = n4; struct node *head,*n1,*n2,*n3,*n4; free(n2); /* creating a new node */ } n1=(struct node *) malloc(sizeof(struct node)); n1->rollno=101; 150 101 NULL n1->next = NULL; head 150 n1-node /* referencing the first node to head pointer */ 150 101 720 102 NULL head = n1; 720 150 /* creating a new node */ n1-node n2-node n2=(struct node *)malloc(sizeof(struct node)); 150 101 720 102 910 104 NULL n2->rollno=102; n2->next = NULL; 150 720 910 n1-node n2-node n3-node /* linking the second node after first node */ n1->next = n2; /* creating a new node * / 150 101 400 102 720 104 NULL n3=(struct node *)malloc(sizeof(struct node)); head 150 400 910n3->rollno=104; n1-node n2-node 103 910 n3-node n3->next=NULL; 720 n4-node /* linking the third node after second node */ n2->next = n3; /* creating a new node */ 150 101 720 103 910 104 NULL n4=(struct node *)malloc (sizeof (struct node)); head 150 720 910 n4->rollno=103; n3-node n1-node n4-node n4->next=NULL; 102 720 /* inserting the new node between 400 second node and third node */ n2-node n2->next = n4;
4. 4. Implementing Singly Linked Liststruct node { } int data; } struct node *next; void insert_after(struct node **h) {}; struct node *new,*temp; int k;struct node *createnode() { if(*h == NULL) return; struct node *new; printf("nEnter data of node after which node : "); new = (struct node *)malloc(sizeof(struct node)); scanf("%d",&k); printf("nEnter the data : "); temp = *h; scanf("%d",&new->data); while(temp!=NULL && temp->data!=k) new->next = NULL; temp = temp->next; return new; if(temp!=NULL) {} new=createnode();void append(struct node **h) { new->next = temp->next; struct node *new,*temp; temp->next = new; new = createnode(); } if(*h == NULL) { } *h = new; void insert_before(struct node **h) { return; struct node *new,*temp,*prev ; } int k; temp = *h; if(*h==NULL) return; while(temp->next!=NULL) temp = temp->next; printf("nEnter data of node before which node : temp->next = new; ");} scanf("%d",&k);void display(struct node *p) { if((*h)->data == k) { printf("nContents of the List : nn"); new = createnode(); while(p!=NULL) { new->next = *h; printf("t%d",p->data); *h = new; return; p = p->next; } temp = (*h)->next; prev = *h;
5. 5. Implementing Singly Linked List ( continued ) while(temp!=NULL && temp->data!=k) { if(temp!=NULL) { prev=temp; prev->next = temp->next; temp=temp->next; free(temp); } } if(temp!=NULL) { } new = createnode(); void search(struct node *h) { new->next = temp; struct node *temp; prev->next = new; int k; } if(h==NULL)return;} printf("nEnter the data to be searched : ");void delnode(struct node **h) { scanf("%d",&k); struct node *temp,*prev; temp=h; int k; while(temp!=NULL && temp->data!=k) if(*h==NULL) return; temp=temp->next; printf("nEnter the data of node to be removed : "); (temp==NULL)? scanf("%d",&k); printf("nt=>Node does not exist") : if((*h)->data==k) { printf("nt=>Node exists"); temp=*h; } *h=(*h)->next; void destroy(struct node **h) { free(temp); struct node *p; return; if(*h==NULL) return; } while(*h!=NULL) { temp=(*h)->next; p = (*h)->next; prev=*h; free(*h); while(temp!=NULL && temp->data!=k) { *h=p; prev=temp; } temp=temp->next; printf("nn ******Linked List is destroyed******"); } }
7. 7. Algorithm for adding two polynomials in linked listsAdd_Polynomial( list p, list q ) set p, q to point to the two first nodes (no headers) initialize a linked list r for a zero polynomial while p != null and q != null if p.exp > q.exp create a node storing p.coeff and p.exp insert at the end of list r advance p else if q.exp > p.exp create a node storing q.coeff and q.exp insert at the end of list r advance q else if p.exp == q.exp if p.coeff + q.coeff != 0 create a node storing p.coeff + q.coeff and p.exp insert at the end of list r advance p, q end while if p != null copy the remaining terms of p to end of r else if q != null copy the remaining terms of q to end of r
9. 9. Insertion of node in Doubly Linked List qA B D C p qA B C D Deletion of node in Doubly Linked ListA B C p D A B C
10. 10. Implementing Doubly Linked Liststruct node { void forward_display(struct node *p) struct node *prev; { int data; printf("nContents of the List : nn"); struct node *next; while(p!=NULL)}; {struct node *createnode() { printf("t%d",p->data); struct node *new; p = p->next; new = (struct node *)malloc(sizeof(struct node)); } printf("nEnter the data : "); printf("n"); scanf("%d",&new->data); } new->prev = NULL; void insert_after(struct node **h) { new->next = NULL; struct node *new,*temp; return new; int k;} if(*h == NULL) return;void append(struct node **h) { printf("nEnter data of node after which node : "); struct node *new,*temp; scanf("%d",&k); new = createnode(); temp = *h; if(*h == NULL) while(temp!=NULL && temp->data!=k) { temp = temp->next; *h = new; if(temp!=NULL) { return; new=createnode(); } new->next = temp->next; temp = *h; temp->next = new; while(temp->next!=NULL) new->prev = temp; temp = temp->next; if(new->next != NULL) temp->next = new; new->next->prev = new; new->prev = temp; }} }
11. 11. Implementing Doubly Linked List ( continued )void insert_before(struct node **h) void delnode(struct node **h){ { struct node *new,*temp; struct node *temp; int k; int k; if(*h==NULL) return; if(*h==NULL) printf("nEnter data of node before which node : "); return; scanf("%d",&k); printf("nEnter the data of node to be removed : "); if((*h)->data == k) { scanf("%d",&k); new = createnode(); if((*h)->data==k) new->next = *h; { new->next->prev=new; temp=*h; *h = new; *h=(*h)->next; return; (*h)->prev=NULL; } free(temp); temp = *h; return; while(temp!=NULL && temp->data!=k) } { temp=*h; temp=temp->next; while(temp!=NULL && temp->data!=k) } { if(temp!=NULL) temp=temp->next; { } new = createnode(); if(temp!=NULL) new->next = temp; { new->prev = temp->prev; temp->next->prev = temp->prev; new->prev->next = new; temp->prev->next = temp->next; temp->prev = new; free(temp); } }} }
14. 14. Implementing Circular Singly Linked Liststruct node { void insert_after(struct node **t) int data; struct node *next; {}; struct node *new,*temp;struct node *createnode() { int k, found=0; struct node *new; if(*t == NULL) return; new = (struct node *)malloc(sizeof(struct node)); printf("nEnter data of node after which node : "); printf("nEnter the data : "); scanf("%d",&k); scanf("%d",&new->data); if((*t)->data==k) new->next = NULL; { return new; new = createnode();} new->next = (*t)->next;void append(struct node **t) { (*t)->next = new; struct node *new,*head; *t=new; new = createnode(); return; if(*t == NULL) { } *t = new; new->next = *t; temp=(*t)->next; return; while(temp!=*t) } { head = (*t)->next; (*t)->next = new; if(temp->data == k) { new->next = head; *t = new; new = createnode();} new->next = temp->next;void display(struct node *t) { temp->next = new; struct node *temp = t->next, *head=t->next; found=1; printf("nContents of the List : nn"); break; do { } printf("t%d",temp->data);temp = temp->next; temp=temp->next; }while(temp!=head); } printf(“n”); if(found==0) printf("nNode does not exist..");} }