Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Computer Programming

Pratik Narang
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

Linked Lists
Self-referential structures
• Each component within the structure includes a pointer indicating
where the next component can be found
• Useful for linked data structures
• Relative ordering of components can be easily altered using pointers
• Addition or deletion is easily done by altering pointers
• No pre-defined maximum size – can grow or shrink
Creating a Linked list
struct node
{
int data;
struct node *next;
};
typedef struct node Node;

Write a program to create a linked list with 3 nodes.


(Use ‘data’ values as 5, 10 and 15)
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head = (Node*)malloc(sizeof(Node));
second = (Node*)malloc(sizeof(Node));
third = (Node*)malloc(sizeof(Node));
head->data = 5;
head->next = second;
second->data = 10;
second->next = third;
third->data = 15;
third->next = NULL;
Traversing a Linked list
• Write a program to print all the data elements of the linked list (of any size) beginning
from the given node

void printList(Node *n)


{
while (n != NULL)
{
printf(" %d ", n->data);
n = n->next;
}
}
Add a node at the end
Node* addNode(Node* head, int value){
Node *temp, *p;
temp = (Node*)malloc(sizeof(Node));
temp->next = NULL;
temp->data = value;
if(head == NULL){
head = temp; }
else{
p = head;//assign head to p
while(p->next != NULL){
p = p->next; }
p->next = temp; }
return head; }
Creating a Linked list (revisited)
• Modify your previous program to create a linked list with N nodes.
if (first != 0)

Solution { temp->next = cur;


temp = cur;}
else
Node* createList(int N) { { first = temp = cur;}
Node *cur, *first, *temp = 0; i++;
int i =0; }
first = 0; temp->next = NULL;
while (i<N) { return first; }
cur = (Node*)malloc(sizeof(Node));
printf("Enter the data item\n");
scanf("%d", &cur-> data);
Add a node at the middle!
• If n is even: insert after n/2th node
• If n is odd: insert after (n+1)/2th node

• void insertAtMid(int value, Node* head)


Node *newNode, *temp;
temp = head;
int len = 0, count = 0;
newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
temp = head;
printf("%d at Mid\n",value);
while (count-- > 1)
while (temp != NULL)
temp = temp->next;
{ temp = temp->next;
newNode->next = temp->next;
len++; }
temp->next = newNode;
count = ((len % 2) == 0)
printList(head);
? (len / 2) : (len + 1) / 2;
Deleting a node from the end
Node* deleteLast(Node* head) if(last == head)
{ head = NULL;
Node *last, *prev; else
if(head == NULL) prev->next = NULL;
printf("List empty."); free(last);
else printf(“Done!");
{ last = head; printList(head);
prev = head; } return head;
while(last->next != NULL) }
{ prev = last;
last = last->next; }
Delete a node anywhere in between
• Node* delete(int value, Node* head)
Node* delete(int value, Node* head) if(flag)
{ Node *toDel, *prev; { if(toDel == head)
int flag=0; head = head->next;
if(head == NULL) else
printf("List is empty."); prev->next = toDel->next;
else free(toDel);
{ toDel = head; }
prev = head; else
while(toDel != NULL){ { printf(“Not found\n”);
if (toDel->data == value) return head; }
{ flag++; printf("%d deleted!\n", value);
break; } printList(head);
prev = toDel; }
toDel = toDel->next; return head;
} }
Unions
• union tag {
• member 1;
• member 2;
•…
• };

• Different data types – same memory location


• Many members
• BUT…. Only one member can contain a value at any given time
• union car
•{
• char name[50];
• int price;
• };

• union car car1, car2, *car3;

You might also like