Interactive C program to implement Instert begining, delete begining and reversing of single linked list
shiv@ubuntu:~/ds/list$ cat revlist.c
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
typedef struct list *L;
/* function to insert element into list */
void insert(L *head)
{
/* look for typedef temp is pointer not variable,
* same will apply to all
*/
L temp;
temp=malloc(sizeof(struct list));
if(!temp) {
printf("Can't allocate memory\n");
return;
}
printf("Enter element to insert\n");
scanf("%d", &temp->data);
temp->next = NULL;
if(*head == NULL) {
*head = temp;
} else {
temp->next = *head;
*head = temp;
}
}
/* function to display elements in the list */
void display(L head)
{
L temp;
temp = head;
if(temp == NULL){
printf("Empty list \n");
return;
}
printf("List elements are \n");
while(temp) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
/* function to delete elements in the list */
void del(L *head)
{
L temp;
if(*head == NULL) {
printf("No elements to delete, Empty list\n");
return;
}
temp =*head;
printf("Element deleted %d\n", temp->data);
*head =(*head)->next;
/* release the memory once deleted */
free(temp);
}
/* function to reverse elements in list */
void reverse(L *head)
{
L temp, t, rev=NULL;
temp = *head;
while(temp != NULL) {
t = temp;
/* first we need to move the list before modifying it */
temp = temp->next;
t->next = rev;
rev = t;
}
/* change head to point reversed list */
*head =rev;
}
/* Main function to help test list operations */
int main()
{
L head = NULL;
int ch;
while(1) {
system("clear");
printf("1 Insert\n2 Display\n3 del\n4 reverse\n5 exit\n");
printf("Enter your choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: insert(&head); break;
case 2: display(head); break;
case 3: del(&head); break;
case 4: reverse(&head); break;
case 5: exit(0);
default: printf("Invalid choice\n");
}
getchar();
getchar();
}
return 0;
}
/* end of the program */
/*
* Interactive C program to simulate following operations
* on single linked list
* 1. insert begining of the list
* 2. display data in the list (traversing)
* 3. delete element at begining of the list
* 4. reverse the elements in single linked list
*/#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
typedef struct list *L;
/* function to insert element into list */
void insert(L *head)
{
/* look for typedef temp is pointer not variable,
* same will apply to all
*/
L temp;
temp=malloc(sizeof(struct list));
if(!temp) {
printf("Can't allocate memory\n");
return;
}
printf("Enter element to insert\n");
scanf("%d", &temp->data);
temp->next = NULL;
if(*head == NULL) {
*head = temp;
} else {
temp->next = *head;
*head = temp;
}
}
/* function to display elements in the list */
void display(L head)
{
L temp;
temp = head;
if(temp == NULL){
printf("Empty list \n");
return;
}
printf("List elements are \n");
while(temp) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
/* function to delete elements in the list */
void del(L *head)
{
L temp;
if(*head == NULL) {
printf("No elements to delete, Empty list\n");
return;
}
temp =*head;
printf("Element deleted %d\n", temp->data);
*head =(*head)->next;
/* release the memory once deleted */
free(temp);
}
/* function to reverse elements in list */
void reverse(L *head)
{
L temp, t, rev=NULL;
temp = *head;
while(temp != NULL) {
t = temp;
/* first we need to move the list before modifying it */
temp = temp->next;
t->next = rev;
rev = t;
}
/* change head to point reversed list */
*head =rev;
}
/* Main function to help test list operations */
int main()
{
L head = NULL;
int ch;
while(1) {
system("clear");
printf("1 Insert\n2 Display\n3 del\n4 reverse\n5 exit\n");
printf("Enter your choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: insert(&head); break;
case 2: display(head); break;
case 3: del(&head); break;
case 4: reverse(&head); break;
case 5: exit(0);
default: printf("Invalid choice\n");
}
getchar();
getchar();
}
return 0;
}
/* end of the program */
Comments
Post a Comment