Sei sulla pagina 1di 11

V.

Data and Results

LINKED LIST PROGRAM NO. 1

#include <stdlib.h>

#include <conio.h>

#include <stdio.h>

struct Node {

int data;

struct Node *next;

};

struct Node* head = NULL;

void insert (int new_data){

struct Node* new_node = (struct Node*)malloc(sizeof(struct


Node));

new_node->data= new_data;

new_node->next= head;

head = new_node;

void display() {

struct Node* ptr;

ptr = head;

while (ptr != NULL) {

printf ("%d ",ptr->data);

ptr = ptr ->next;

int main() {

clrscr();

insert(3);
insert(1);

insert(7);

insert(2);

insert(9);

printf("The linked list is: ");

display();

getch();

return 0;

Fig. 1 is a C language program that


displays the linked list numbers.
LINKED LIST PROGRAM NO. 2

#include<iostream.h>

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

node *next;

};

class list

private:

struct node *head, *tail;

public:

list()

node *head=NULL;

node *tail=NULL;

void createnode(int value)

struct node *temp=new node;

temp->data=value;

temp->next=NULL;

if(head==NULL)

head=temp;

tail=temp;

temp=NULL;
}

else

tail->next=temp;

tail=temp;

void display()

struct node *temp=new node;

temp=head;

while (temp!=NULL)

printf ("%d\t",temp->data);

temp=temp->next;

void insert_start(int value)

node *temp=new node;

temp->data=value;

temp->next=head;

head=temp;

void insert_position(int pos, int value)

node *pre=new node;

node *cur=new node;

node *temp=new node;

cur=head;

for(int i=1;i<pos;i++)
{

pre=cur;

cur=cur->next;

temp->data=value;

pre->next=temp;

temp->next=cur;

void delete_first()

node *temp=new node;

temp=head;

head=head->next;

delete temp;

void delete_last()

node *current=new node;

node *previous=new node;

current=head;

while(current->next!=NULL)

previous=current;

current=current->next;

tail=previous;

previous->next=NULL;

delete current;

void delete_position(int pos)

{
node *current=new node;

node *previous=new node;

current=head;

for(int i=1;i<pos;i++)

previous=current;

current=current->next;

previous->next=current->next;

};

int main()

clrscr();

list obj;

obj.createnode(25);

obj.createnode(50);

obj.createnode(90);

obj.createnode(40);

printf("\n--------------------------------------------------\n");

printf("---------------Displaying All nodes---------------\n");

obj.display();

cout<<"\n-----------------Inserting At End------------------\n";

obj.createnode(55);

obj.display();

cout<<"\n----------------Inserting At Start----------------\n";

obj.insert_start(50);

obj.display();

cout<<"\n-------------Inserting At Particular--------------\n";

obj.insert_position(5,60);

obj.display();
cout<<"\n----------------Deleting At Start-----------------\n";

obj.delete_first();

obj.display();

cout<<"\n-----------------Deleting At End-------------------\n";

obj.delete_last();

obj.display();

cout<<"\n--------------Deleting At Particular--------------\n";

obj.delete_position(4);

obj.display();

cout<<"\n--------------------------------------------------\n";

system ("pause");

return 0;

Fig. 2 is a linked list program that displays all the numbers and then inserts
a number at the start, end and at the particular position of the original set
of numbers. This program also deletes number at the start, end and at
particular position.
LINKED LIST PROGRAM NO. 3

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

struct node {

int num;

struct node *nextptr;

}*stnode;

void createNodeList (int n);

void displayList ();

int main(){

int n;

printf("\n\nLinked List: To create and display Singly Linked


List:\n");

printf("------------------------------------------------\n");

printf("Input the number of nodes: ");

scanf ("%d", &n);

createNodeList(n);

printf("\nDate entered in the list: \n";

displayList();

getch();

return 0;

void createNodeList(int n){

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node*)malloc(struct node));


if (stnode==NULL){

printf("Memory can not be allocated.");

else{

printf("Input data for 1: ");

scanf("%d", &num);

stnode->num=num;

stnode->nextptr = NULL;

tmp= stnode;

for (i=2;i<=n;i++){

fnNode = (struct node*)malloc(sizeof(struct node));

if(fnNode==NULL){

printf("Memory can not be allocated");

break;

else {

printf("Input data for node %d: ",i);

scanf("%d", &num);

fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

void displayList(){
struct node *tmp;

if (stnode==NULL){

printf("List is empty.");

else{

tmp=stnode;

while (tmp!=NULL){

printf("Data = %d\n", tmp->num);

tmp = tmp->nextptr;

Fig. 3 lets the user to input the number of nodes and also the data for
the nodes. This program also displays all the numbers inputted by the
user.

Potrebbero piacerti anche