0% found this document useful (0 votes)
7 views6 pages

C Program for Linear Linked List

The document contains three C programs: one for implementing a linear linked list with various operations such as insertion and deletion, another for traversing a binary tree using inorder, preorder, and postorder methods, and a third for reading and sorting city names alphabetically. Each program includes necessary functions and a main function to demonstrate their usage. The code is structured with proper memory management and user input handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

C Program for Linear Linked List

The document contains three C programs: one for implementing a linear linked list with various operations such as insertion and deletion, another for traversing a binary tree using inorder, preorder, and postorder methods, and a third for reading and sorting city names alphabetically. Each program includes necessary functions and a main function to demonstrate their usage. The code is structured with proper memory management and user input handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a c program to implement linear linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL , *newnode;

void printList()
{
struct node *temp=head;
printf("\n linked list \n");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
if(temp!=NULL)
{
printf(" , ");
}
}
printf("]\n");
}
void insertAtBegin(int data)
{
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=data;
newnode->next=head;
head=newnode;
}

void insertAtEnd(int data)


{
struct node *newnode , *last;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
return;
}
last=head;
while(last->next!=NULL)
{
last=last->next;
}
last->next=newnode;
}
void insertAfterNode(struct node *prenode , int data)
{
struct node *newnode;
if(prenode==NULL)
{
printf("\n previous node can't be NULL\n");
return;
}
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=prenode->next;
prenode->next=newnode;
}
void deleteAtBegin()
{
struct node *temp;
if(head==NULL)
{
printf("\n list is already empty \n");
return ;
}
temp=head;
head=head->next;
free(temp);
}
void deleteAtEnd()
{
struct node *last ,*prev;
if(head==NULL)
{
printf("\n list is already empty \n");
return;
}
last=head;
prev=NULL;
while(last->next!=NULL)
{
prev=last;
last=last->next;
}
if(prev==NULL)
{
head=NULL;
}
else
{
prev->next=NULL;
}
free(last);
}
void deleteNode( int key)
{
int data;
struct node *temp=head;
struct node *prev=NULL;
if(temp!=NULL && temp->data==key)
{
head=temp->next;
free(temp);
return ;
}
while(temp!=NULL && temp->data!=key)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("\n key not found in the list \n");
return;
}
prev->next=temp->next;
free(temp);
}
int searchList(int key)
{
struct node *temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
return 1;
}
temp=temp->next;
}
return 0;
}
int main()
{
int key;
clrscr();
insertAtBegin(12);
insertAtBegin(22);
insertAtEnd(30);
insertAtEnd(44);
insertAtBegin(50);
insertAfterNode(head->next->next , 33);
printf("\n Initial linked list \n");
printList();
deleteAtBegin();
deleteAtEnd();
deleteNode(12);
printf("\n linked list after deletion \n");
printList();
insertAtBegin(4);
insertAtBegin(16);
printf("\n updated linked list \n");
printList();
key=16;
if(searchList(key)==1)
{
printf("\n element %d is found " , key);
}
return 0;
}

Write a c program to display traversal of tree

#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *left;
struct node *right;
};
void inorderTraversal(struct node *root)
{
if(root==NULL)
return;
inorderTraversal(root->left);
printf("%d->",root->item);
inorderTraversal(root->right);
}
void preorderTraversal(struct node *root)
{
if(root==NULL)
return;
printf("%d->",root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node *root)
{
if(root==NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d->",root->item);
}
struct node *createnode(value)
{
struct node *newnode=malloc(sizeof(struct node));
newnode->item=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node *insertLeft(struct node *root , int value)
{
root->left=createnode(value);
return root->left;
}
struct node *insertRight(struct node *root , int value)
{
root->right=createnode(value);
return root->right;
}
void main()
{
struct node *root=createnode(1);
clrscr();
insertLeft(root,12);
insertRight(root,9);
insertLeft(root->left,5);
insertRight(root->left,6);
printf("\n Inorder Traversal \n");
inorderTraversal(root);
printf("\n preorder traversal ");
preorderTraversal(root);
printf("\n postorder traversal \n");
postorderTraversal(root);
getch();
}

Write a c program to read the names of cities & arrange them n alphabetically.

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n;
char str[100][100],s[100];
clrscr();
printf("\n Enter the number of names \n");
scanf("%d",&n);
printf("\n enter the names in any order \n");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\n the sorted order of names are :\n");
for(i=0;i<n;i++)
{
printf("%s \t",str[i]);
}
getch();
return 0;
}

You might also like