Successfully reported this slideshow.
Upcoming SlideShare
×

# Unit7 C

2,188 views

Published on

• Full Name
Comment goes here.

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

### Unit7 C

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 Linear Non-Linear Stacks Queues Trees Graphs Linear Lists Some Data structures Arrays Strings Lists Stacks Queues Trees Graphs Dictionaries Maps Hash Tables Sets Lattice Neural-Nets Some Common Operations on Data structures Insertion : adding a new element to the collection. Deletion : removing an element from a collection. Traversal : access and examine each element in collection. Search : find whether an element is present or not. Sorting : rearranging elements in a particular order. Merging : combining two collections into one collection.
3. 3. struct node { int rollno; struct node *next; }; int main() { struct node *head,*n1,*n2,*n3,*n4; /* creating a new node */ n1=(struct node *) malloc(sizeof(struct node)); n1->rollno=101; n1->next = NULL; /* referencing the first node to head pointer */ head = n1; /* creating a new node */ n2=(struct node *)malloc(sizeof(struct node)); n2->rollno=102; n2->next = NULL; /* linking the second node after first node */ n1->next = n2; /* creating a new node * / n3=(struct node *)malloc(sizeof(struct node)); n3->rollno=104; n3->next=NULL; /* linking the third node after second node */ n2->next = n3; /* creating a new node */ n4=(struct node *)malloc (sizeof (struct node)); n4->rollno=103; n4->next=NULL; /* inserting the new node between second node and third node */ n2->next = n4; n4->next = n3; /* deleting n2 node */ n1->next = n4; free(n2); } Creating a Singly Linked List 150 head 150 400 720 910 n1-node n2-node n4-node n3-node 150 n1-node 150 head 150 150 n1-node n2-node 720 150 150 n1-node n2-node 720 910 n3-node 150 head 150 400 720 910 n1-node n2-node n4-node n3-node 101 400 102 720 103 910 104 NULL 101 NULL 101 720 102 NULL 101 720 102 910 104 NULL 101 720 102 720 103 910 104 NULL
4. 4. struct node { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;nEnter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; } void display(struct node *p) { printf(&quot;nContents of the List : nn&quot;); while(p!=NULL) { printf(&quot;t%d&quot;,p->data); p = p->next; } } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf(&quot;nEnter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; } } void insert_before(struct node **h) { struct node *new,*temp,*prev ; int k; if(*h==NULL) return; printf(&quot;nEnter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data == k) { new = createnode(); new->next = *h; *h = new; return; } temp = (*h)->next; prev = *h; Implementing Singly Linked List
5. 5. while(temp!=NULL && temp->data!=k) { prev=temp; temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; prev->next = new; } } void delnode(struct node **h) { struct node *temp,*prev; int k; if(*h==NULL) return; printf(&quot;nEnter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; free(temp); return; } temp=(*h)->next; prev=*h; while(temp!=NULL && temp->data!=k) { prev=temp; temp=temp->next; } if(temp!=NULL) { prev->next = temp->next; free(temp); } } void search(struct node *h) { struct node *temp; int k; if(h==NULL)return; printf(&quot;nEnter the data to be searched : &quot;); scanf(&quot;%d&quot;,&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; (temp==NULL)? printf(&quot;nt=>Node does not exist&quot;) : printf(&quot;nt=>Node exists&quot;); } void destroy(struct node **h) { struct node *p; if(*h==NULL) return; while(*h!=NULL) { p = (*h)->next; free(*h); *h=p; } printf(&quot;nn ******Linked List is destroyed******&quot;); } Implementing Singly Linked List ( continued )
7. 7. Add_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 Algorithm for adding two polynomials in linked lists
9. 9. A B C D A B D C q p q A B C D p A B C Insertion of node in Doubly Linked List Deletion of node in Doubly Linked List
10. 10. struct node { struct node *prev; int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;nEnter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->prev = NULL; new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; new->prev = temp; } void forward_display(struct node *p) { printf(&quot;nContents of the List : nn&quot;); while(p!=NULL) { printf(&quot;t%d&quot;,p->data); p = p->next; } printf(&quot;n&quot;); } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf(&quot;nEnter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; new->prev = temp; if(new->next != NULL) new->next->prev = new; } } Implementing Doubly Linked List
11. 11. void insert_before(struct node **h) { struct node *new,*temp; int k; if(*h==NULL) return; printf(&quot;nEnter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data == k) { new = createnode(); new->next = *h; new->next->prev=new; *h = new; return; } temp = *h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; new->prev = temp->prev; new->prev->next = new; temp->prev = new; } } void delnode(struct node **h) { struct node *temp; int k; if(*h==NULL) return; printf(&quot;nEnter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; (*h)->prev=NULL; free(temp); return; } temp=*h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { temp->next->prev = temp->prev; temp->prev->next = temp->next; free(temp); } } Implementing Doubly Linked List ( continued )