⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 link.c

📁 simple link list program
💻 C
字号:
/*********** program to build a single link list ******************************/
#include<stdio.h>
struct node* create_link(int);
void display_link(void);
void add_node(int);
void delete_link(int);
void search_data(int);

//struct represent a node, contain data and linkess of next node(struct)//
	struct node {
		int data;
		struct node *link; // linkess of next node
	};
static struct node *first = 0; // pointer to struct
int i;

// number of nodes u want to creat
int n=0,location =0;

/* ******************************************************
* main 
*********************************************************/
void main()
{
    int del_node=0,sdata=0;
	printf("how many node u want\n");
	printf("size of node %d\n",sizeof(struct node));
	scanf("%d",&n);
	first = create_link(n);
	printf("display details of all node\n");
	display_link();
	printf("where u want to add a new node????\n ");
	printf("give serial no 0->begining,1->after first node or last\n ");
	scanf("%d",&location);
	add_node(location);
	printf("display modified list\n");
	display_link();
	printf("enter which node u want to delete???\n");
	scanf("%d",&del_node);
	delete_link(del_node);
	display_link();
	printf("searching an element from linked list??? enter element \n");
	scanf("%d",sdata);
	search_data(sdata);
	display_link();
	
	
	
}
/* ******************************************************
* receives number of nodes to be created
* return the address of first node(base addr of firsr structure)
*
*********************************************************/
struct node* create_link(int num)
{
    struct node* temp= 0;
	//create first node,so allocate memory dynamically for first node//
	first = (struct node*) malloc(sizeof(struct node));
	//enter data to first node//
	printf("enter data to first node\n");
	scanf("%d",&first->data);
	
/*create next node: to retain the base linkess of(first)take one temporary 
veriable(temp) and assign it to first
*/
	temp = first;
	for(i = 0; i<=num-1; i++) {
	if(i == num-1)
	temp->link =NULL;
	else {
	
	//allocate memory for 2nd node where @first node pointing
	temp->link =(struct node*) malloc(sizeof(struct node)*10);
	temp = temp->link;
	printf("enter data to next node \n");
	scanf("%d",&temp->data);
//	temp = temp->link; dont put it here data ll store to the next node
	}
	}
return first;
}
/****************************************************
* add node at any location
*
*******************************************************/
void add_node(int loc)
{
	struct node *new= 0,*ptr=0;
//at begining
if(loc ==0)
{
	new = (struct node*)malloc(sizeof(struct node));
	printf("enter data to this link\n");
	scanf("%d",&new->data);
	new->link = first;
	first = new;

}

// in bw
else if (loc < n)
	{
	printf("want to add a node after %d node\n",loc);
	ptr = first;
	new = (struct node*)malloc(sizeof(struct node));
	printf("enter data to this link\n");
	scanf("%d",&new->data);
	for(i=0;i<loc-1;i++)
	ptr = ptr->link;
	new->link = ptr->link;
	ptr->link = new;
}

//at the end 
	else if(loc == n){
	printf("add a node at the end\n");//at the end
	new = (struct node*)malloc(sizeof(struct node));
	printf("enter data to this link\n");
	scanf("%d",&new->data);
	ptr = first;
	while(ptr->link)
	ptr = ptr->link;
	ptr->link = new;
	new->link = NULL;

 }
 
}

/****************************************************
* delete node
*
*******************************************************/ 
void delete_link(int dnode){
	struct node  *temp=0,*save=0;
	printf("want to delete %d :node\n",dnode);
	if (dnode == 1) 
	{
	 temp = first;
	 save = first->link;
	 first = first->link;
	 free(temp);
 	 } 
	 
	 	else if(dnode < n )
	 	{
	 	 temp = first;
		 save = first;
	 	  for(i =1;i<dnode;i++){
	 	     temp = temp->link;}
   	 	  for(i =1; i<dnode+1;i++){
	 	     save = save->link;}
	 	      temp->link = save->link;
	 	    free(save );
	 	  }
	 	  
	else if(dnode == n) {
		printf("last node to delete\n");
		temp = first;
	    save = first;
	    while(temp->link != NULL){
	    save = temp;
	    temp = temp->link;
	    }
	    save->link = NULL;
	    free(temp);
	   	}

}


/****************************************************
* delete node
*
*******************************************************/ 
void search_data(int sdata)
	{
		struct node *temp;
		temp = first;
		 while(temp->link != NULL){
		 while(temp->data != sdata )
		 temp = temp->link;
		 }
		 
		

	}

 
/****************************************************
* display all nodes
*
*******************************************************/
void display_link(void)
{
struct node *x;
x = first;
while(x){
printf("data %d\t link for next node %d\n",x->data,x->link);
x = x->link;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -