Linkedlist
Linkedlist
Info Link
ptr
• }
Spring 2012 Programming and Data Structure 15
Spring 2012 Programming and Data Structure 16
• Void insert_given_info()
• {
• Struct node *ptr;
• Int data;
• Ptr=(struct node*)malloc(sizeof(struct node));
• If(ptr==null)
• {
• printf(“Overflow”);
• Return;
• }
• Printf(“Enter new node information”);
• Scanf(“%d”,&ptr->info);
• Printf(“input information of node after want to insert”);
• Scanf(“%d”&data);
• Cpt=first;
• While(cpt->info!=data)
• {
• Cpt=cpt->link;
• }
• Ptr->link=Cpt->link;
• cpt->link=ptr;
• }
Spring 2012 Programming and Data Structure 17
• To delete the node from the existing linked list
before we have to check the underflow
condition.
• Like insertion ,deletion is also possible from
following 3 position.
• From the beginning
• From the end
• If information of node is given
A B C
A B C
A B C
A B C
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Spring 2012 Programming and Data Structure 38
List is an Abstract Data Type
• What is an abstract data type?
– It is a data type defined by the user.
– Typically more complex than simple data types like
int, float, etc.
• Why abstract?
– Because details of the implementation are hidden.
– When you do some operation on the list, say insert
an element, you just call a function.
– Details of how the list is implemented or how the
insert function is written is no longer required.
Spring 2012 Programming and Data Structure 39
Illustration: Deletion
Item to be deleted
A B C
tmp
curr
A B C
Insert
List
implementation
Delete
and the
related functions
Traverse
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
A B C
node *head;
………
head = create_list();
p = head;
while (p != NULL)
{
printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p->age);
count++;
p = p->next;
}
printf ("\n");
}
node *head;
………
display (head);
p = *head;
{
q = p;
p = p->next;
} The pointers
q and p
if (p == NULL) /* At the end */ always point
{ to consecutive
q->next = new; nodes.
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
Spring 2012 Programming and Data Structure 60
• To be called from main() function as:
node *head;
………
insert (&head);
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
{
q = p;
p = p->next;
}
B A
C B A
C B A B C
Also called a
STACK
sub
mul Complex
Number
div
read
print
Spring 2012 Programming and Data Structure 71
Example 2 :: Set manipulation
struct node {
int element; Structure
struct node *next;
definition
}
typedef struct node set;
intersect
minus
Set
insert
delete
size
Spring 2012 Programming and Data Structure 73
Example 3 :: Last-In-First-Out STACK
Assume:: stack contains integer elements
pop
create
STACK
isempty
isfull
dequeue
create
QUEUE
isempty
size
PUSH
top
top
POP
top
top
PUSH OPERATION
top
POP OPERATION
top
ARRAY
Spring 2012 Programming and Data Structure 88
void push (stack **top, int element)
{
stack *new;
new = (stack *) malloc(sizeof(stack));
if (new == NULL)
{
printf (“\n Stack is full”);
exit(-1);
}
new->value = element;
new->next = *top;
*top = new;
}
LINKED LIST
Spring 2012 Programming and Data Structure 89
Popping an element from the stack
int pop (stack *s)
{
if (s->top == -1)
{
printf (“\n Stack underflow”);
exit(-1);
}
else
{
return (s->st[s->top--]);
}
}
ARRAY
main() if (isempty(B))
{ printf (“\n B is
stack *A, *B; empty”);
create(&A); create(&B); }
push(&A,10);
push(&A,20);
ENQUEUE
front rear
DEQUEUE
front rear
struct node{
char name[30];
struct node *next;
};
typedef struct {
_QNODE *queue_front, *queue_rear;
} _QUEUE;
init_queue(&q);
command[0]='\0';
printf("For entering a name use 'enter <name>'\n");
printf("For deleting use 'delete' \n");
printf("To end the session use 'bye' \n");
while(strcmp(command,"bye")){
scanf("%s",command);
ENQUEUE DEQUEUE
0 N
front
front rearrear
typedef struct {
_ELEMENT q_elem[MAX_SIZE];
int rear;
int front;
int full,empty;
} _QUEUE;
q->rear=(q->rear+1)%(MAX_SIZE);
q->q_elem[q->rear]=ob;
return;
}
q->front=(q->front+1)%(MAX_SIZE);
temp=q->q_elem[q->front];
return(temp);
}
Spring 2012 Programming and Data Structure 110
Queue Example: Contd.
main() #include <stdio.h>
{ #include <stdlib.h>
int i,j; #include <string.h>
char command[5];
_ELEMENT ob;
_QUEUE A;
init_queue(&A);
command[0]='\0';
printf("For adding a name use 'add [name]'\n");
printf("For deleting use 'delete' \n");
printf("To end the session use 'bye' \n");
Spring 2012 Programming and Data Structure 111
Queue Example: Contd.
while (strcmp(command,"bye")!=0){
scanf("%s",command);
if(strcmp(command,"add")==0) {
scanf("%s",ob.name);
if (IsFull(&A))
printf("No more insertion please \n");
else {
AddQ(&A,ob);
printf("Name inserted %s \n",ob.name);
}
}